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

后端 未结 9 2044
旧巷少年郎
旧巷少年郎 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 16:05

    The setHours method can take optional minutes, seconds and ms arguments, for example:

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

    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.

提交回复
热议问题