0

I want to convert the returned value of new Date to UTC format using moment js.

I tried using moment.utc but

moment.utc(moment(new Date())).format();
output -> 2020-01-01T04:00:00Z

but I want output in this format Thu Jun 04 2020 00:00:15 GMT+0530 (India Standard Time) just like what new Date returns but it should be in UTC.

and the other problem is that the return type of format is of

TYPE : STRING I want that the format should be the same but its type should be of date object.

Any help would be appreciated.

Rakesh
  • 63
  • 3
  • 11
  • UTC is a time standard, not a format. – RobG Jun 03 '20 at 20:41
  • `moment().utc().format('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ')`. Probably a duplicate of [*How to format a JavaScript date*](https://stackoverflow.com/questions/3552461/how-to-format-a-javascript-date?r=SearchResults&s=1|1450.4192). – RobG Jun 03 '20 at 22:31
  • Thanks @RobG for your reply but the issue here is that the format method returns it as a string but I want it to be returned as Date type. – Rakesh Jun 05 '20 at 10:17
  • I think you're confused about how timezones work. Dates are just a time value, an offset in milliseconds from 1970-01-01 00:00:00Z. They do not have a timezone or any other information, *getTimezoneOffset* returns the system timezone offset, it's not a property of a Date. Local values are calculated for output, they aren't properties of a Date either. Moment.js with timezone support can associate a different timezone with a Date, e.g. moment().utc() returns an object where all values will be UTC by default instead of local. – RobG Jun 05 '20 at 14:00

0 Answers0