【问题标题】:Calculate two time with milliseconds using jQuery使用 jQuery 以毫秒计算两次时间
【发布时间】:2021-04-20 14:49:44
【问题描述】:

我有两次

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>

【问题讨论】:

  • 这已经在stackoverflow.com/questions/11883768/…中得到回答
  • @RahulShukla 这不符合我的预期。
  • 如果您希望它以0:0:0.002 的格式显示,为什么要将其拆分为小时和分钟?

标签: javascript jquery momentjs


【解决方案1】:
  • 将您的自定义日期字符串转换为有效的 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>

【讨论】:

    【解决方案2】:

    您可以简单地使用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)}`);
    &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.2/moment.js"&gt;&lt;/script&gt;

    请注意虽然是probably time to stop using momentjs

    【讨论】:

    • 如果日、月或年不同怎么办?
    • @RokoC.Buljan OP 中的两个示例都没有这个细节。 0:0:0.002 的预期格式也没有考虑到它(据我所知是 hrs:mins:secs:ms)
    • 通常是hh* 承担全部负载 - 因为没有更高的y m d
    猜你喜欢
    • 1970-01-01
    • 2013-05-05
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多