125 lines
4.2 KiB
JavaScript
125 lines
4.2 KiB
JavaScript
// 秒数转化为时分秒(未补零)
|
||
function formatSeconds(value) {
|
||
// 秒
|
||
let second = parseInt(value)
|
||
// 分
|
||
let minute = 0
|
||
// 小时
|
||
let hour = 0
|
||
// 天
|
||
// let day = 0
|
||
// 如果秒数大于60,将秒数转换成整数
|
||
if (second > 60) {
|
||
// 获取分钟,除以60取整数,得到整数分钟
|
||
minute = parseInt(second / 60)
|
||
// 获取秒数,秒数取佘,得到整数秒数
|
||
second = parseInt(second % 60)
|
||
// 如果分钟大于60,将分钟转换成小时
|
||
if (minute > 60) {
|
||
// 获取小时,获取分钟除以60,得到整数小时
|
||
hour = parseInt(minute / 60)
|
||
// 获取小时后取佘的分,获取分钟除以60取佘的分
|
||
minute = parseInt(minute % 60)
|
||
// 如果小时大于24,将小时转换成天
|
||
// if (hour > 23) {
|
||
// // 获取天数,获取小时除以24,得到整天数
|
||
// day = parseInt(hour / 24)
|
||
// // 获取天数后取余的小时,获取小时除以24取余的小时
|
||
// hour = parseInt(hour % 24)
|
||
// }
|
||
}
|
||
}
|
||
return [hour, minute, second]
|
||
}
|
||
|
||
// 秒数转化为时分秒(补零)
|
||
function formatSecondsZero(value) {
|
||
let second = parseInt(value); // 秒
|
||
let minute = 0; // 分
|
||
let hour = 0; // 时
|
||
if (second > 59) {
|
||
minute = parseInt(second / 60)
|
||
second = parseInt(second % 60)
|
||
if (minute > 59) {
|
||
hour = parseInt(minute / 60)
|
||
minute = parseInt(minute % 60)
|
||
if (hour < 10) hour = '0' + hour; //小时补零
|
||
}
|
||
}
|
||
if (second < 10) second = '0' + second; //秒数补零
|
||
if (minute < 10) minute = '0' + minute; //分钟补零
|
||
let result = '' + second;
|
||
result = '' + minute + ':' + result
|
||
if (hour > 0) {
|
||
result = '' + hour + ':' + result
|
||
}
|
||
console.log(hour,minute,second);
|
||
return result
|
||
}
|
||
|
||
|
||
function optionTitleNum(index) {
|
||
var letter = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", ]
|
||
return letter[index];
|
||
}
|
||
|
||
//获取当前时间(年月日时分秒 yy-mm-dd hh:mm:ss)
|
||
function getCurrentDateTime() {
|
||
const now = new Date();
|
||
const year = now.getFullYear();
|
||
const month = (now.getMonth() + 1).toString().padStart(2, '0');
|
||
const day = now.getDate().toString().padStart(2, '0');
|
||
const hours = now.getHours().toString().padStart(2, '0');
|
||
const minutes = now.getMinutes().toString().padStart(2, '0');
|
||
const seconds = now.getSeconds().toString().padStart(2, '0');
|
||
|
||
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
|
||
}
|
||
|
||
//获取当前时间(年月日时分秒 保存到数组)
|
||
function getCurrentDateTimeArr() {
|
||
const now = new Date();
|
||
const year = now.getFullYear();
|
||
const month = (now.getMonth() + 1).toString().padStart(2, '0');
|
||
const day = now.getDate().toString().padStart(2, '0');
|
||
const hours = now.getHours().toString().padStart(2, '0');
|
||
const minutes = now.getMinutes().toString().padStart(2, '0');
|
||
const seconds = now.getSeconds().toString().padStart(2, '0');
|
||
|
||
return [year, month, day, hours, minutes, seconds];
|
||
}
|
||
|
||
//以当前时间减去制定日期计算相差的时间
|
||
function calculateIntervalTime(startTime,endTime) {
|
||
const startData = Date.parse(startTime)/1000
|
||
const endData = Date.parse(endTime)/1000
|
||
let resData = ''
|
||
|
||
if(startData < endData){
|
||
resData = endData - startData
|
||
}
|
||
if(startData == endData){
|
||
return 0
|
||
}
|
||
// var day = Math.floor(resData / ( 3600 * 24))
|
||
// var hour = Math.floor((resData - day*3600*24) / 3600)
|
||
// var minute = Math.floor((resData - day*3600*24 - 3600*hour) / 60)
|
||
// var second = Math.floor(resData - day*3600*24 - 3600*hour - 60*minute)
|
||
var second = Math.floor(resData)
|
||
return second;
|
||
}
|
||
|
||
//比较两时间大小
|
||
function compare(time1, time2) {
|
||
if(time1 != "" && time2 != ""){
|
||
if(new Date(time1).getTime() > new Date(time2).getTime()){
|
||
return 0;
|
||
}else if(new Date(time1).getTime() < new Date(time2).getTime()){
|
||
return 1;
|
||
}else{
|
||
return 2;
|
||
}
|
||
}else{
|
||
return -1;
|
||
}
|
||
} |