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

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

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

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 16:00

    Just going to add this here because I landed on this page looking for how to do this in moment.js and others may do too.

    [Rationale: the word "moment" already appears elsewhere on this page so search engines direct here, and moment.js is widespread enough to warrant to being covered going on how often it is mentioned in other date-related SO questions]

    So, in version 2.0.0 and above:

    date.startOf('day');
    

    For earlier versions:

    date.sod();
    

    Docs:

    http://momentjs.com/docs/#/manipulating/start-of/

提交回复
热议问题