-1

I want to set a timezone to a date. So basically a date should be display everytime taking into account that timezone. Also i want to use a native solution. I know that exists this solution:

const t = new Date(1641991591447).toLocaleString('en-GB', { timeZone: 'Europe/London' })
console.log(t)

But this returns 12/01/2022, 12:46:31 instead of 2022-01-12T12:46:31.447Z this format. So how to get the last format taking into account the timezone?

Asking
  • 3,487
  • 11
  • 51
  • 106
  • `2022-01-12T12:46:31.447Z` is not a locale format. That is an ISO string, which can be retrieved using `t.toISOString()` – evolutionxbox Jan 12 '22 at 14:28
  • @evolutionxbox, i know this is why i asked how to get that formt using the timezone. – Asking Jan 12 '22 at 14:29
  • May you provide an example using another timezone (one which preferably isn't UTC+0)? – evolutionxbox Jan 12 '22 at 14:30
  • @evolutionxbox, basically the solution what i am looking for is next: Having this date `new Date(1641991591447)` i need to decrement/increment the date according to UK timezone. So the output also should look like this format `1641991591447`, but already incremented/decremented. So when i will read the output with `new Date(the processed value)` i have to get the UK time. Please let me know if you did not understand. – Asking Jan 12 '22 at 14:43
  • `1641991591447` is an exact time (no timezone). The timezone is calculated on output. the format you're asking for is the [zulu timezone](https://www.timeanddate.com/time/zones/z) (the `z` in the date). – evolutionxbox Jan 12 '22 at 14:46
  • @evolutionxbox, i want to do something like this var myDate = new Date(); myDate.setHours(myDate.getHours() + nr of hours); console.log(myDate); – Asking Jan 12 '22 at 14:49
  • Please note that `Date` objects do not have timezones. See [How to initialize a JavaScript Date to a particular time zone](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone). If you want to add hours to a date, see [Adding hours to JavaScript Date object?](https://stackoverflow.com/questions/1050720/adding-hours-to-javascript-date-object) – evolutionxbox Jan 12 '22 at 15:34

1 Answers1

0

In order to convert your date to the specified format (ISO date string), you can do this by simply creating a Date constructor and using your date value as the input and chaining the toISOString method to it.

const t = new Date(1641991591447).toISOString(); // 2022-12-01T12:46:31.000Z
nick-w-nick
  • 147
  • 1
  • 12
  • This doesn't make sense? All this code does is remove the milliseconds from the date. The OP might as well just use `new Date(1641991591447).toISOString()` – evolutionxbox Jan 12 '22 at 15:19
  • @evolutionxbox, but how to be sure that the timezone every time will be set to UK ? – Asking Jan 12 '22 at 15:21
  • I do not think ISO dates have timezones. – evolutionxbox Jan 12 '22 at 15:23
  • I don't understand why `nick-w-nick`'s answer is not correct. – Asking Jan 12 '22 at 15:24
  • @evolutionxbox, then how to set the timezone? – Asking Jan 12 '22 at 15:31
  • Maybe if you show an example that doesn't use a UTC+0 time zone, since at the moment the code does very little – evolutionxbox Jan 12 '22 at 15:31
  • @Asking `Date` objects do not have timezones. See [How to initialize a JavaScript Date to a particular time zone](https://stackoverflow.com/questions/15141762/how-to-initialize-a-javascript-date-to-a-particular-time-zone) – evolutionxbox Jan 12 '22 at 15:33
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/241004/discussion-between-asking-and-evolutionxbox). – Asking Jan 12 '22 at 15:35
  • @evolutionxbox You are correct, I got mixed up on the conversion since the input has no timezone. The answer has been edited for clarification. Thanks! – nick-w-nick Jan 12 '22 at 15:37