.setHours(0,0,0,0) with moment.js
Asked Answered
M

5

78

What is the easiest equivalent of date.setHours(0,0,0,0) when using moment.js?

Basically, if I need a new Date() moment with hours, minutes, seconds, and milliseconds zeroed out.

Mehala answered 18/2, 2015 at 19:28 Comment(0)
M
201

Start of Time

moment().startOf('day')
Mehala answered 18/2, 2015 at 19:40 Comment(1)
if you want to transform it toDate, you can do it like this moment(date).startOf("day").toDate()Malvin
Z
4

Suppose date is the moment object which contains the date having some hours and minutes.

You can use the following code to reset hours, minutes and seconds to 0.

date.utcOffset(0);
date.set({
 hour: 0,
 minute: 0,
 second: 0,
 millisecond: 0,
});
Zachar answered 18/2, 2015 at 19:28 Comment(0)
V
1

to set second for example to " 0 " time = moment().second(0);

Vilberg answered 26/1, 2021 at 15:38 Comment(0)
P
1

If you want in string format, you can use like this: moment().format("YYYY-MM-DD 00:00:00")

Panicle answered 18/5, 2022 at 9:32 Comment(0)
M
0

let todayDate = moment(0, "HH")

Mannered answered 20/2, 2023 at 7:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.