What is the best way to initialize a JavaScript Date to midnight?

后端 未结 9 2030
旧巷少年郎
旧巷少年郎 2020-11-29 15:38

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

9条回答
  •  再見小時候
    2020-11-29 15:46

    In case you already have d3.js as a dependency in your project, or don't mind bringing it in, d3-time (d3.js library is modular as of v4.0.0) has got Intervals.

    They might prove useful when setting dates to "default" values, e.g. midnight, 0.00 seconds, the first of the month, etc.

    var d = new Date(); // Wed Aug 02 2017 15:01:07 GMT+0200 (CEST)
    d3.timeHour(d) // Wed Aug 02 2017 00:00:00 GMT+0200 (CEST)
    d3.timeMonth(d) // Tue Aug 01 2017 00:00:00 GMT+0200 (CEST)
    

提交回复
热议问题