If you want a date constructor for specific timezones make use of new Date('YYYY-MM-DDTHH:NN:SS.sss±HH:NN')
.
What you describe and want, is what Date already does if you generate a Date using new Date('YYYY-MM-DDTHH:NN:SS.sss±HH:NN')
. You simply have to use it in this format.
±HH:NN
is timezone information from your timezone-dropdown. It can either start with a + or a -. (Take a look at the 3rd snippet)
Other formats as the ones i mentioned, are invalid if passed to the Date constructor and you may get unexpected results.
The dropdown (the one about the timezone) requires the information of +hrs:mins
and -hrs:mins
respective to the timezone selected.
The following describes the Date constructor.
Don't pass a formated string like this to new Date()
- it's invalid.
If you use new Date()
you either have to pass a time value in milliseconds, a date only or an ISO Timestamp.
new Date(date.valueOf())
results in a copy
new Date(date.toISOString())
results in a copy
new Date(date.toJSON())
results in a copy
new Date(date.toDateString())
results in a copy of the date only => no hour GMT +0 on the current day.
All examples above (except the last one) are a copy of the original date.
For more information on the arguments supported by the Date-constructor take a look at Date.parse()#Date Time String Format
There is no need to change anything. If it's midnight at Asia/Karachi, it will be 9pm (21:00) in Europe/Prague.
As you can see on this example, use GMT+0
or any other accepted Time-Zone String.
Examples of different arguments
1
const date = new Date("2021-02-14T00:00:05.000+00:00"); // 5 seconds after midnight on February 14th, 2021 GMT+0
["GMT+0", "UTC", "America/New_York", "Europe/Madrid", "Europe/Prague", "Europe/Moscow", "Asia/Karachi", "Asia/Tokyo", "PRC"].forEach(timeZone => {
console.log(timeZone, date.toLocaleString('en-US', { timeZone }))
});
2
const date = new Date("2021-09-14");
["GMT+0", "UTC", "America/New_York", "Europe/Madrid", "Europe/Prague", "Europe/Moscow", "Asia/Karachi", "Asia/Tokyo", "PRC"].forEach(timeZone => {
console.log(timeZone, date.toLocaleString('en-US', { timeZone }))
});
3
// 16:00 at timezone +9 is 7 am at gmt
const date = new Date("2021-09-14T16:00:00.000+09:00");
["GMT+0", "UTC", "America/New_York", "Europe/Madrid", "Europe/Prague", "Europe/Moscow", "Asia/Karachi", "Asia/Tokyo", "PRC"].forEach(timeZone => {
console.log(timeZone, date.toLocaleString('en-US', { timeZone }))
});