Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set 00:00:00 using moment.js [duplicate]

I want to get current date but time should be 00:00:00.000

I've try this:

var m = moment(); m.set({hour:0,minute:0,second:0,millisecond:0}); console.log(m.toISOString()); 

but I've got: 2016-01-12T23:00:00.000Z why 23 and not 00?

like image 949
jcubic Avatar asked Jan 13 '16 09:01

jcubic


People also ask

How do I set the default timezone in moments?

To reset the default time zone to local, use moment. tz. setDefault with no arguments.

What is moment () hour ()?

The moment(). hour() Method is used to get the hours from the current time or to set the hours. Syntax: moment().hour(); or. moment().


2 Answers

Moment.js stores dates it utc and can apply different timezones to it. By default it applies your local timezone. If you want to set time on utc date time you need to specify utc timezone.

Try the following code:

var m = moment().utcOffset(0); m.set({hour:0,minute:0,second:0,millisecond:0}) m.toISOString() m.format() 
like image 52
Volodymyr Synytskyi Avatar answered Sep 23 '22 17:09

Volodymyr Synytskyi


var time = moment().toDate();  // This will return a copy of the Date that the moment uses  time.setHours(0); time.setMinutes(0); time.setSeconds(0); time.setMilliseconds(0); 
like image 33
saikumar Avatar answered Sep 24 '22 17:09

saikumar