0

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.

5
  • 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? Commented Jun 7, 2024 at 14:29
  • @evolutionxbox Also .toISOString() will create a UTC Timestring, ie something like '2024-06-07T14:33:20.542Z' where the Z is denoting that this is to be interpreted as UTC ... Commented Jun 7, 2024 at 14:33
  • a Date in 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 ... Commented Jun 7, 2024 at 14:37
  • it again shows the local time - this is just how you're looking at it, not what it stored in the Date object. Commented Jun 7, 2024 at 14:45
  • 1
    @anushka-shrestha Can you clarify this part 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/… Commented Jun 7, 2024 at 15:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.