如何在Javascript中从午夜开始计算ms [英] How to calculate ms since midnight in Javascript

查看:201
本文介绍了如何在Javascript中从午夜开始计算ms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

计算自ms(午夜)以来(最后)午夜过去的时间的最佳方法是什么?

What is the best way to calculate the time passed since (last) midnight in ms?

推荐答案

创建新日期使用当前日/月/年,并获得差异。

Create a new date using the current day/month/year, and get the difference.

var now = new Date(),
    then = new Date(
        now.getFullYear(),
        now.getMonth(),
        now.getDate(),
        0,0,0),
    diff = now.getTime() - then.getTime(); // difference in milliseconds

这篇关于如何在Javascript中从午夜开始计算ms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆