4

I have seen a lot, really a lot of post to find out a solution for my problem, but i couldn't get it, so decided to create a question.

My Question is how do we actually create a new javascript date object in different timezone, not in local timezone

i know we could create local date object and convert it to different timezone in number of ways, but i dont want to convert instead i need to create in specific timezone.

here is simple example for my problem,

Say, user has choosen a timezone "America/New_york", so all the dates in a calendar page will be shown in that timezone.

Now, if we create a event at "05:00 pm" , how do we actually create date with time 5 pm in "America/New_york" timezone,

if we use new Date() (assume browser is in different timezone say "Asia/Kolkata"), then converting it to "America/New_york" will not get "5:00pm" in that timezone , instead it will get corresponding time of "05:00 pm IST" in that timezone which will have different hour & minute value.

Any suggestion would be helpful!

Thanks

0

1 Answer 1

2

The short answer is you can't.

A Date object is just an accessor to the system time settings (so it will use the local computer timezone anyway). You can then manipulate your dates by substracting the local timezone using getTimezoneOffset(), or forcing a time with setUTCHours().

Note that moment.js is a good alternative to handle dates and timezones: http://momentjs.com/

Sign up to request clarification or add additional context in comments.

3 Comments

That is correct, JS does not let you set the timezone for a Date
Hi Ribon, i am using momentjs , but it will not help us to create date in a timezone , rather it is used to convert date to different timezone, i can use moment(any date).tz('timezone').format() to get differnt timezone date string or moment().utc() to get the utc value, but none of them is correct for the above scenario i have mentioned thats y i have raised a question
Also, may i know why this question is marked as duplicate, those suggested post doesn't tell any way to create rather those are asking to convert to timezones , but that is not the answer for the question asked,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.