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

Ramesh Lingappa
  • 2,448
  • 20
  • 33

1 Answers1

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/

floribon
  • 19,175
  • 5
  • 54
  • 66
  • That is correct, JS does not let you set the timezone for a Date – Ruan Mendes Jan 03 '14 at 21:54
  • 2
    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 – Ramesh Lingappa Jan 04 '14 at 07:30
  • 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, – Ramesh Lingappa Jan 04 '14 at 07:34