function countdown(startTime, timeFrame) {
let staytimeGap = 0
if (new Date().getTime() <= (new Date(startTime).getTime() + timeFrame * 60 * 1000)) {
staytimeGap = (new Date(startTime).getTime() + timeFrame * 60 * 1000) - new Date().getTime()
}
return staytimeGap
}
let time = new Date('2022-05-11T10:50:43.000Z')
console.log(new Date().getTime(), new Date(time).getTime() + 15 * 60 * 1000, new Date().getTime() - new Date(time).getTime())
let t = countdown(time, 15)
console.log(t % 60 % 1000)
//时间格式化
Date.prototype.Format = function (fmt) {
var o = {
"M+": this.getMonth() + 1,
"d+": this.getDate(),
"h+": this.getHours(),
"m+": this.getMinutes(),
"s+": this.getSeconds(),
"q+": Math.floor((this.getMonth() + 3) / 3),
"S": this.getMilliseconds()
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
//计算时间差-分钟
function TimeDifference(time1, time2) {
//判断开始时间是否大于结束日期
if (time1 > time2) {
alert("开始时间不能大于结束时间!");
return false;
}
//截取字符串
var begin1 = time1.substr(0, 10).split("-");
var end1 = time2.substr(0, 10).split("-");
//将拆分的数组重新组合,并实例成化新的日期对象
var date1 = new Date(begin1[0] + - + begin1[1] + - + begin1[2]);
var date2 = new Date(end1[0] + - + end1[1] + - + end1[2]);
//得到两个日期之间的差值m,以分钟为单位
var m = parseInt(Math.abs(date2 - date1) / 1000 / 60);
//小时数和分钟数相加得到总的分钟数
//time1.substr(11,2)截取字符串得到时间的小时数
//parseInt(time1.substr(11,2))*60把小时数转化成为分钟
var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));
//两个分钟数相减得到时间部分的差值,以分钟为单位
var n = min2 - min1;
var sec1 = parseInt(time1.substr(17, 2)) / 60;
var sec2 = parseInt(time2.substr(17, 2)) / 60;
var s = sec2 - sec1;
//将日期和时间两个部分计算出来的差值相加,即得到两个时间相减后的分钟数
var minutes = m + n + s;
return minutes.toFixed(2);
}
//时间-加分钟数
function addMinutes(t, m) {
var time = new Date(t);
time.setMinutes(time.getMinutes() + m, time.getSeconds(), 0);
return time;
}
// console.log(TimeDifference(new Date().getTime()), new Date(time))
var startTime = "2022-1-1 11:00:21";
var endTime = "2022-1-1 10:50:13"; // 11:05:00
//计算时间差(相差分钟)
function timeDifference(startTime, endTime) {
// 年份日期是否大
let s1 = startTime.split(" ");
let e1 = endTime.split(" ");
let yS1 = s1[0].split("-")
let yE1 = e1[0].split("-")
for (let i = 0; i <= 2; i++) {
if (yS1[i] > yE1[i]) {
return 0
}
}
// 时分秒
let start1 = s1[1].split(":");
let startAll = parseInt(start1[0] * 60) + parseInt(start1[1]);
let sSecd = parseInt(start1[2])
let end1 = e1[1].split(":");
let endAll = parseInt(end1[0] * 60 + 15) + parseInt(end1[1]);
let eSecd = parseInt(end1[2])
// 时间差
let diffTime = (endAll - startAll)*60*1000
if (sSecd <= eSecd) {
diffTime = diffTime + (eSecd-sSecd)*1000
} else {
diffTime = diffTime - 1*60*1000 + (eSecd+60-sSecd)*1000
}
console.log(diffTime%60%1000, 'min')
return diffTime
}
console.log(timeDifference(startTime, endTime),)
console