Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UTC Date object in NodeJS? [duplicate]

I want to get the Current Date object in UTC. I tried using new Date(), Date.now() etc. But they return the local time. How do I get the UTC date object?

I want the Date object, not string representation.

like image 260
Nimish David Mathew Avatar asked Apr 04 '18 08:04

Nimish David Mathew


People also ask

How do I get UTC in node JS?

To get a UTC Date object in Node. js and JavaScript, we can call the date's toUTCString method. to create a new Date instance and call toUTCString to return the UTC date string version of the current date time.

How do you make a UTC date?

JavaScript Date getUTCDate() getUTCDate() returns the day of the month (1 to 31) of a date object. getUTCDate() returns the day according to UTC.

Is new date () getTime () UTC?

Use the getTime() method to get a UTC timestamp, e.g. new Date(). getTime() . The method returns the number of milliseconds since the Unix Epoch and always uses UTC for time representation. Calling the method from any time zone returns the same UTC timestamp.

How to get current date and time in Node JS?

In Node.js date and time are handled with the Javascript Date object. It is loaded by default and requires no import of modules. Also you can get current date, month, year, hour, minutes & seconds in Node.js. In Node.js you can get the current date-time formatted as YYYY-MM-DD hh:mm:ss, YYYY-MM-DD, DD-MM-YYYY etc.

How to get current date and time from a date object?

The current date and time can be fetched by first creating a new Dateobject. Thereafter methods of the object can be called to get the date and time values. // new Date object let date_ob = new Date();

What is UTC time in JavaScript?

UTC (Universal Time Coordinated) is the time set by the World Time Standard. UTC time is the same as GMT time (Greenwich Mean Time). All JavaScript getUTC methods assume that the date is of local time.

What is the use of UTC () method in date?

Date.UTC() returns a time value as a number instead of creating a Date object. If a parameter is outside of the expected range, the UTC() method updates the other parameters to accommodate the value. For example, if 15 is used for month, the year will be incremented by 1 (year + 1) and 3 will be used for the month. UTC()...


1 Answers

Just use new Date(new Date().toUTCString())

After converting to UTC, we have to again convert it into Date object if we need a Date Object.

like image 180
Enric A. Avatar answered Oct 04 '22 01:10

Enric A.