使用 jQuery 以毫秒计算两次时间
Calculate two time with milliseconds using jQuery
我有两次
1. 04/20/2021 19:25:20.2522888 + 05:30
2. 04/20/2021 19:25:20.2692870 + 05:30
需要计算JavaScript或Jquery
的时间差
格式示例输出:0:0:0.002
var startTime = moment("12:16:59 am", "HH:mm:ss a");
var endTime = moment("06:12:07 pm", "HH:mm:ss a");
var duration = moment.duration(endTime.diff(startTime));
var hours = parseInt(duration.asHours());
var minutes = parseInt(duration.asMinutes()) - hours * 60;
alert(hours + ' hour and ' + minutes + ' minutes.')
var result = endTime.diff(startTime, 'hours') + " Hrs and " +
endTime.diff(startTime, 'minutes') + " Mns";
alert(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.js"></script>
- 将您的自定义日期字符串转换为有效的 ISO8601 格式
- 获取两个日期之间的毫秒差
- 以
h*:m:s.ms
格式构建自定义响应
const myDateFormatToISO8601 = str => {
const [M, D, Y, h, m, s, ms, tzh, tzm] = str.split(/\W+/);
const ms3 = Math.round(ms / 1e4); // Round ms to 3 integers
const ISO8601 = `${Y}-${M}-${D}T${h}:${m}:${s}.${ms3}+${tzh}:${tzm}`;
return new Date(ISO8601);
};
const calcDiff = (Date_a, Date_b) => {
const a = myDateFormatToISO8601(Date_a);
const b = myDateFormatToISO8601(Date_b);
const d = Math.abs(b - a);
const h = ~~(d / 36e5);
const m = ~~(d / 6e5) % 60;
const s = ~~(d / 1e3) % 60;
const ms = d % 1e3;
return `${h}:${m}:${s}.${ms}`;
};
// DEMO TIME:
const calculate = () => {
const a = document.querySelector("#a").value;
const b = document.querySelector("#b").value;
document.querySelector("#result").textContent = calcDiff(a, b);
};
document.querySelectorAll(".date").forEach(EL => EL.addEventListener("input", calculate));
calculate();
input {width: 90%;}
<input id="a" class="date" value="04/20/2021 19:25:20.1522888 + 05:30"><br>
<input id="b" class="date" value="04/20/2021 19:25:20.2692870 + 05:30">
<div id="result"></div>
你可以简单地使用 diff
var startTime = moment("19:25:20.2522888","hh:mm:ss.SSSSSSS");
var endTime = moment("19:25:20.2692870","hh:mm:ss.SSSSSSS");
var hrs = endTime.diff(startTime,"hours");
var mins = endTime.diff(startTime,"minutes");
var secs = endTime.diff(startTime,"seconds");
var ms = endTime.diff(startTime,"milliseconds");
console.log(`${hrs}:${mins}:${secs}.${ms.toString().padStart(3,0)}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.js"></script>
我有两次
1. 04/20/2021 19:25:20.2522888 + 05:30
2. 04/20/2021 19:25:20.2692870 + 05:30
需要计算JavaScript或Jquery
的时间差格式示例输出:0:0:0.002
var startTime = moment("12:16:59 am", "HH:mm:ss a");
var endTime = moment("06:12:07 pm", "HH:mm:ss a");
var duration = moment.duration(endTime.diff(startTime));
var hours = parseInt(duration.asHours());
var minutes = parseInt(duration.asMinutes()) - hours * 60;
alert(hours + ' hour and ' + minutes + ' minutes.')
var result = endTime.diff(startTime, 'hours') + " Hrs and " +
endTime.diff(startTime, 'minutes') + " Mns";
alert(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.js"></script>
- 将您的自定义日期字符串转换为有效的 ISO8601 格式
- 获取两个日期之间的毫秒差
- 以
h*:m:s.ms
格式构建自定义响应
const myDateFormatToISO8601 = str => {
const [M, D, Y, h, m, s, ms, tzh, tzm] = str.split(/\W+/);
const ms3 = Math.round(ms / 1e4); // Round ms to 3 integers
const ISO8601 = `${Y}-${M}-${D}T${h}:${m}:${s}.${ms3}+${tzh}:${tzm}`;
return new Date(ISO8601);
};
const calcDiff = (Date_a, Date_b) => {
const a = myDateFormatToISO8601(Date_a);
const b = myDateFormatToISO8601(Date_b);
const d = Math.abs(b - a);
const h = ~~(d / 36e5);
const m = ~~(d / 6e5) % 60;
const s = ~~(d / 1e3) % 60;
const ms = d % 1e3;
return `${h}:${m}:${s}.${ms}`;
};
// DEMO TIME:
const calculate = () => {
const a = document.querySelector("#a").value;
const b = document.querySelector("#b").value;
document.querySelector("#result").textContent = calcDiff(a, b);
};
document.querySelectorAll(".date").forEach(EL => EL.addEventListener("input", calculate));
calculate();
input {width: 90%;}
<input id="a" class="date" value="04/20/2021 19:25:20.1522888 + 05:30"><br>
<input id="b" class="date" value="04/20/2021 19:25:20.2692870 + 05:30">
<div id="result"></div>
你可以简单地使用 diff
var startTime = moment("19:25:20.2522888","hh:mm:ss.SSSSSSS");
var endTime = moment("19:25:20.2692870","hh:mm:ss.SSSSSSS");
var hrs = endTime.diff(startTime,"hours");
var mins = endTime.diff(startTime,"minutes");
var secs = endTime.diff(startTime,"seconds");
var ms = endTime.diff(startTime,"milliseconds");
console.log(`${hrs}:${mins}:${secs}.${ms.toString().padStart(3,0)}`);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.js"></script>