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

后端 未结 9 2038
旧巷少年郎
旧巷少年郎 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:01

    A one-liner for object configs:

    new Date(new Date().setHours(0,0,0,0));
    

    When creating an element:

    dateFieldConfig = {
          name: "mydate",
          value: new Date(new Date().setHours(0, 0, 0, 0)),
    }
    

提交回复
热议问题