0

I've a timezone. say GMT+6.30. now I want to add/minus few hours to it? How to do that! I've tried every way that I can find on net. I got the timezone from earth tool.

Here is what I tried.

I tried to set the time with date object and use .getTimezoneOffset(); but it didn't return based on time that I set. Rather showing local timezone.

Suppose I've set london date time in a date object and used date.getTimezoneOffset(); and I'm on timezone +5, then it's showing +5 not 00

I have also thought manual adding, that's a big work. So what I need is to know that is there any function to add/minus some minutes and hours to timezone?? I can also get the +0630 formatted timezone, will it help to easily add few hours?

Or can you suggest me a easy way to get the timezone from a date time?

AtanuCSE
  • 8,832
  • 14
  • 74
  • 112
  • Can you provide some code illustrating your question? – Ortomala Lokni Jun 30 '14 at 07:03
  • @OrtomalaLokni Exactly what type of code you need?? I need to add few minutes/hours to timezone like +6.00 or -5.30. The methods I tried, failed. Can't find a easy way to do this. I want to know if there is any easy way to do it, like some library or function, or do I need to create a big algorithm to do it. I don't know what type of code could be given in this context. I've shared that I tried. – AtanuCSE Jun 30 '14 at 14:45
  • This is clearly an [XY Problem](http://meta.stackexchange.com/a/66378/201534). Please include details about what you are attempting to achieve, rather than just the difficulty you are having with your attempted solution. Thanks. – Matt Johnson-Pint Jun 30 '14 at 17:18
  • Possibly related (but hard to tell from current wording of your question): [How to initialize javascript date to a particular timezone](http://stackoverflow.com/q/15141762/634824) – Matt Johnson-Pint Jun 30 '14 at 17:19
  • @MattJohnson I've a timezone, for example GMT+2.30 or GMT-11, I need to add or subtract from that timezone. Hope I made myself clear. Why this is needed? Cause I've the timezone, and the offset of the day light saving. And I specifically need to add/substract to/from the timezone not the time. Why! because I'll need to use it in an api. Tell me if any more explanation is needed. – AtanuCSE Jun 30 '14 at 18:02
  • Sorry, but I still don't understand what you're actually trying to do. What specifically are you sending to the API? A date adjusted to a time zone offset? Or just the time zone offset? Why do you want to add or subtract from it? What is the *intent*? Also "time zone" isn't the same thing as a "time zone offset". You may want to read the [timezone tag wiki](http://stackoverflow.com/tags/timezone/info). – Matt Johnson-Pint Jun 30 '14 at 18:43
  • I've attempted to answer your question based on the information thus far provided. If this doesn't address the intent of what you're trying to achieve, please edit your question accordingly. Thanks. – Matt Johnson-Pint Jun 30 '14 at 18:54

1 Answers1

0

You said:

I tried to set the time with date object and use .getTimezoneOffset(); but it didn't return based on time that I set. Rather showing local timezone.

Suppose I've set london date time in a date object and used date.getTimezoneOffset(); and I'm on timezone +5, then it's showing +5 not 00

That is the sole purpose of the getTimezoneOffset function. It returns the local time zone offset of the computer the code is running on, as a number of minutes west of GMT for the specific date and time in question. There is no way to change this behavior.

You've asked how to add or subtract from this value. You certainly could do that, since it is just a number. But it would not achieve much. The date object would still represent its date and time using millseconds from the Unix epoch internally, and would still present itself using the local time zone of the computer it was running on.

There are solutions for working with other-than-local time zones in JavaScript, but most require use of a library and external time zone data. I list several of these libraries here.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575