查找两个日期之间的天数、小时数和分钟数之间的时差
Find time difference between 2 dates with days, hours and minutes
假设我有一个包含以下所有数据的数组列表。
let events = {
["-1 19:00"],
["-1 20:00"],
["-1 17:00", "-1 23:00"],
["1 18:00"],
["2 18:00"],
["3 18:00"],
["4 18:00"],
["5 18:00"],
["6 18:00"],
["7 18:00"],
};
所以,这里的-1代表每一天,就像每个“星期天,星期一”等等。 -1 = 每天,1 = 星期一,2 = 星期二,依此类推。
所以,我想计算从当前时间到最近一天的剩余时间,数组中包含小时和分钟。我真的不知道我应该怎么做。
getTimeLeftTillDay(dayname, time) :any {
let d = new Date();
let coming = parseInt(dayname);
if(coming === -1) {
coming = d.getDay();
}
const day = d.getDay();
const targetDay = coming; // Someday
let dayOffset = targetDay - day;
if (dayOffset < 0) dayOffset += 7;
d = new Date(d.getTime() + (dayOffset * 24 * 3600 * 1000));
let timea = parseInt(time[0]);
let timeb = parseInt(time[1]);
d.setHours(timea);
d.setMinutes(timeb);
return d;
}
我尝试使用上面的代码,但它没有像我预期的那样工作。我真的很喜欢帮助!
如果您可以依赖第 3 方库,我会使用众多日期库之一来执行计算。以下是使用 date-fns:
的方法
import {addDays, formatDuration, intervalToDuration, setHours, setMinutes} from "date-fns";
const getTimeLeftTillDay = (dayName, time) => {
let daysToAdd = 0;
if (dayName !== '-1') {
daysToAdd = Number.parseInt(dayName);
}
const startDate = new Date();
let endDate = addDays(startDate, daysToAdd);
const [minutes, seconds] = time.split(":");
const hoursToSet = Number.parseInt(minutes);
endDate = setHours(endDate, hoursToSet)
const minutesToSet = Number.parseInt(seconds);
endDate = setMinutes(endDate, minutesToSet)
return customFormatDuration(startDate, endDate);
}
export const customFormatDuration = (start, end) => {
const durations = intervalToDuration({start, end})
return formatDuration(durations);
}
console.log(getTimeLeftTillDay("-1", "19:00"));
console.log(getTimeLeftTillDay("1", "02:00"));
console.log(getTimeLeftTillDay("7", "02:00"));
这会在我的机器上打印以下内容(在欧洲中部时间 2:25 下午执行):
4 hours 35 minutes
11 hours 35 minutes
6 days 21 hours 35 minutes
如果你想考虑计算时间早于当前时间的情况,并且你想将它们视为“第二天”并一般处理过去的日子,你可以这样做:
const getTimeLeftTillDay = (dayName, time) => {
const startDate = new Date();
let dayToSet = getDay(startDate);
if (dayName !== '-1') {
dayToSet = Number.parseInt(dayName) - 1;
}
let endDate = new Date();
const [minutes, seconds] = time.split(":");
const hoursToSet = Number.parseInt(minutes);
endDate = setHours(endDate, hoursToSet)
const minutesToSet = Number.parseInt(seconds);
endDate = setMinutes(endDate, minutesToSet)
endDate = setDay(endDate, dayToSet);
if (isBefore(endDate, startDate)) {
endDate = addDays(endDate, 1);
}
return customFormatDuration(startDate, endDate);
}
假设我有一个包含以下所有数据的数组列表。
let events = {
["-1 19:00"],
["-1 20:00"],
["-1 17:00", "-1 23:00"],
["1 18:00"],
["2 18:00"],
["3 18:00"],
["4 18:00"],
["5 18:00"],
["6 18:00"],
["7 18:00"],
};
所以,这里的-1代表每一天,就像每个“星期天,星期一”等等。 -1 = 每天,1 = 星期一,2 = 星期二,依此类推。
所以,我想计算从当前时间到最近一天的剩余时间,数组中包含小时和分钟。我真的不知道我应该怎么做。
getTimeLeftTillDay(dayname, time) :any {
let d = new Date();
let coming = parseInt(dayname);
if(coming === -1) {
coming = d.getDay();
}
const day = d.getDay();
const targetDay = coming; // Someday
let dayOffset = targetDay - day;
if (dayOffset < 0) dayOffset += 7;
d = new Date(d.getTime() + (dayOffset * 24 * 3600 * 1000));
let timea = parseInt(time[0]);
let timeb = parseInt(time[1]);
d.setHours(timea);
d.setMinutes(timeb);
return d;
}
我尝试使用上面的代码,但它没有像我预期的那样工作。我真的很喜欢帮助!
如果您可以依赖第 3 方库,我会使用众多日期库之一来执行计算。以下是使用 date-fns:
的方法import {addDays, formatDuration, intervalToDuration, setHours, setMinutes} from "date-fns";
const getTimeLeftTillDay = (dayName, time) => {
let daysToAdd = 0;
if (dayName !== '-1') {
daysToAdd = Number.parseInt(dayName);
}
const startDate = new Date();
let endDate = addDays(startDate, daysToAdd);
const [minutes, seconds] = time.split(":");
const hoursToSet = Number.parseInt(minutes);
endDate = setHours(endDate, hoursToSet)
const minutesToSet = Number.parseInt(seconds);
endDate = setMinutes(endDate, minutesToSet)
return customFormatDuration(startDate, endDate);
}
export const customFormatDuration = (start, end) => {
const durations = intervalToDuration({start, end})
return formatDuration(durations);
}
console.log(getTimeLeftTillDay("-1", "19:00"));
console.log(getTimeLeftTillDay("1", "02:00"));
console.log(getTimeLeftTillDay("7", "02:00"));
这会在我的机器上打印以下内容(在欧洲中部时间 2:25 下午执行):
4 hours 35 minutes
11 hours 35 minutes
6 days 21 hours 35 minutes
如果你想考虑计算时间早于当前时间的情况,并且你想将它们视为“第二天”并一般处理过去的日子,你可以这样做:
const getTimeLeftTillDay = (dayName, time) => {
const startDate = new Date();
let dayToSet = getDay(startDate);
if (dayName !== '-1') {
dayToSet = Number.parseInt(dayName) - 1;
}
let endDate = new Date();
const [minutes, seconds] = time.split(":");
const hoursToSet = Number.parseInt(minutes);
endDate = setHours(endDate, hoursToSet)
const minutesToSet = Number.parseInt(seconds);
endDate = setMinutes(endDate, minutesToSet)
endDate = setDay(endDate, dayToSet);
if (isBefore(endDate, startDate)) {
endDate = addDays(endDate, 1);
}
return customFormatDuration(startDate, endDate);
}