将JavaScript日期初始化为午夜的最佳方法是什么? [英] What is the best way to initialize a JavaScript Date to midnight?

查看:106
本文介绍了将JavaScript日期初始化为午夜的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取新Date()实例的最简单方法是什么,但是将时间设置为午夜?

What is the simplest way to obtain an instance of new Date() but set the time at midnight?

推荐答案

setHours 方法可以选择分钟 ms 参数,例如:

var d = new Date();
d.setHours(0,0,0,0);

这会将时间设置为 00:00:00.000 您当前的时区,如果您想在UTC时间工作,可以使用 setUTCHours method。

That will set the time to 00:00:00.000 of your current timezone, if you want to work in UTC time, you can use the setUTCHours method.

这篇关于将JavaScript日期初始化为午夜的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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