I am trying to show current UTC time rather than the local time using new Date().
Using (new Date()).toUTCString() does give the UTC string but I need the UTC object as I need to pass the value to other libraries as internally the libraries converts it into Date Object. So every time I convert it into UTC time with the string wrapped into new Date, it again shows the local time +GMT+200. Is there any way I can get +GMT000 date object irrespective of the timezone I am in?
I am trying to get a new Date() object that always returns +GMT000 irrespective of the timezone I am at.
new Date()is a UTC object. Local timezones are calculated when formatting to a string.new Date().toISOString()may be what you're looking for?.toISOString()will create a UTC Timestring, ie something like'2024-06-07T14:33:20.542Z'where theZis denoting that this is to be interpreted as UTC ...Datein Javascript is no more than the number of milliseconds since a certain point in time (1970-01-01T00:00:00Z), so it's in fact already a "UTC Object". However, how it is displayed to the client is a complete different story. So if you need something like the "current time"new Date()is perfectly fine and can be passed around as you like. You only have to be careful when displaying that to the user, ie what is displayed and how may the user interpret that ...I need the UTC object as I need to pass the value to other libraries as internally the libraries convert it into Date Object? Date Object holds the UTC value. Reference: "Note: While the time value at the heart of a Date object is UTC, the basic methods to fetch the date and time or its components all work in the local (i.e. host system) time zone and offset." MDN Date Javascript developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…