两次使用dayjs的区别
Difference between two time using dayjs
我有两个时间输入,我想使用 dayjs
获得这两个时间之间的 difference/time-interval
fromtime = '13:00'
totime = '17:00'
所以上面两个的输出应该是4:00
小时
我试过了
console.log(
dayjs(fromtime).diff(dayjs(totime), "hours")
);
但没有得到预期的输出。
Dayjs 期望 Date 采用某种格式 (dayjs parse string) not just a time. However you can set the hour (dayjs set hour) without setting a certain date (dayjs parse now):
var fromtime = dayjs().hour(13)
var totime = dayjs().hour(17)
console.log(totime.diff(fromtime, "hours"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.20/dayjs.min.js"></script>
编辑
What if the input contains fromtime = '10.25' and totime = '11.30'. So my output should be '1.05'. But when I follow your method the output is 1. Is there a way to solve this
您也可以设置分钟 (dayjs set minute)。不幸的是,我在该库中看不到任何时差格式选项。所以我们必须自己计算:
function formatInterval(minutes) {
let interval = [
Math.floor(minutes / 60).toString(), //hours ("1" - "12")
(minutes % 60).toString() //minutes ("1" - "59")
];
return interval[0].padStart(2, '0') + ':' + interval[1].padStart(2, '0')
}
let fromtime = dayjs().hour(10).minute(25);
let totime = dayjs().hour(11).minute(30);
let interval = totime.diff(fromtime, "minute");
console.log(formatInterval(interval));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.20/dayjs.min.js"></script>
EDIT2
This will fail if the day switches between the two first lines
OK 假设 fromtime
将始终小于 totime
... 如果不是这样,我们可以从总分钟数中减去负数分钟数像这样的一天:
function formatInterval(minutes) {
let interval = [Math.floor(minutes / 60).toString(), (minutes % 60).toString()];
return interval[0].padStart(2, '0') + ':' + interval[1].padStart(2, '0')
}
function getInterval(from, to) {
let [hoursA, minutesA] = from.split(':');
let [hoursB, minutesB] = to.split(':');
let timeA = dayjs().hour(hoursA).minute(minutesA);
let timeB = dayjs().hour(hoursB).minute(minutesB);
let interval = timeB.diff(timeA, 'minutes');
if(interval < 0) {
return formatInterval(24 * 60 + timeB.diff(timeA, 'minutes'));
}
return formatInterval(interval);
}
console.log(getInterval('23:00', '1:45'));
console.log(getInterval('10:25', '11:30'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.20/dayjs.min.js"></script>
我找到了解决方法。
const fromtime = '11:20'
const totime = '12:30'
const ft = dayjs(`2000-01-01 ${fromtime}`);
const tt = dayjs(`2000-01-01 ${totime}`);
const mins = tt.diff(ft, "minutes", true);
const totalHours = parseInt(mins / 60);
const totalMins = dayjs().minute(mins).$m
这将给出输出 totalHours = 1
和 totalMins = 10
。
希望这对某人有所帮助。
fromtime = '13:00'
totime = '17:00'
这些目前是字符串,您需要将其转换为整数。
console.log(parseInt(fromtime) - parseInt(totime)) //4
我有两个时间输入,我想使用 dayjs
获得这两个时间之间的 difference/time-intervalfromtime = '13:00'
totime = '17:00'
所以上面两个的输出应该是4:00
小时
我试过了
console.log(
dayjs(fromtime).diff(dayjs(totime), "hours")
);
但没有得到预期的输出。
Dayjs 期望 Date 采用某种格式 (dayjs parse string) not just a time. However you can set the hour (dayjs set hour) without setting a certain date (dayjs parse now):
var fromtime = dayjs().hour(13)
var totime = dayjs().hour(17)
console.log(totime.diff(fromtime, "hours"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.20/dayjs.min.js"></script>
编辑
What if the input contains fromtime = '10.25' and totime = '11.30'. So my output should be '1.05'. But when I follow your method the output is 1. Is there a way to solve this
您也可以设置分钟 (dayjs set minute)。不幸的是,我在该库中看不到任何时差格式选项。所以我们必须自己计算:
function formatInterval(minutes) {
let interval = [
Math.floor(minutes / 60).toString(), //hours ("1" - "12")
(minutes % 60).toString() //minutes ("1" - "59")
];
return interval[0].padStart(2, '0') + ':' + interval[1].padStart(2, '0')
}
let fromtime = dayjs().hour(10).minute(25);
let totime = dayjs().hour(11).minute(30);
let interval = totime.diff(fromtime, "minute");
console.log(formatInterval(interval));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.20/dayjs.min.js"></script>
EDIT2
This will fail if the day switches between the two first lines
OK 假设 fromtime
将始终小于 totime
... 如果不是这样,我们可以从总分钟数中减去负数分钟数像这样的一天:
function formatInterval(minutes) {
let interval = [Math.floor(minutes / 60).toString(), (minutes % 60).toString()];
return interval[0].padStart(2, '0') + ':' + interval[1].padStart(2, '0')
}
function getInterval(from, to) {
let [hoursA, minutesA] = from.split(':');
let [hoursB, minutesB] = to.split(':');
let timeA = dayjs().hour(hoursA).minute(minutesA);
let timeB = dayjs().hour(hoursB).minute(minutesB);
let interval = timeB.diff(timeA, 'minutes');
if(interval < 0) {
return formatInterval(24 * 60 + timeB.diff(timeA, 'minutes'));
}
return formatInterval(interval);
}
console.log(getInterval('23:00', '1:45'));
console.log(getInterval('10:25', '11:30'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/dayjs/1.8.20/dayjs.min.js"></script>
我找到了解决方法。
const fromtime = '11:20'
const totime = '12:30'
const ft = dayjs(`2000-01-01 ${fromtime}`);
const tt = dayjs(`2000-01-01 ${totime}`);
const mins = tt.diff(ft, "minutes", true);
const totalHours = parseInt(mins / 60);
const totalMins = dayjs().minute(mins).$m
这将给出输出 totalHours = 1
和 totalMins = 10
。
希望这对某人有所帮助。
fromtime = '13:00'
totime = '17:00'
这些目前是字符串,您需要将其转换为整数。
console.log(parseInt(fromtime) - parseInt(totime)) //4