0

My requirement is , i need to convert the time zone value to offset value for ex.. Asia/Dubai to +4 .. consider am intializing date in js.

var currentDate = new Date();

and it is possible to set the time zone and get the offset..

currentDate.setTimezone("Asia/Dubai");
var offsetValue=currentDate.getTimezoneOffset();

alert('offsetValue---'+offsetValue);

its not working.. page error at set time zone.. is there any other way to get the offet value? Need to consider DST too..

4
  • 2
    What's your opinion regarding daylight savings time? Commented Apr 25, 2012 at 4:48
  • use timezone-js. this will make your work simpler .. Commented Apr 25, 2012 at 5:03
  • can u share me that? timezone.js Commented Apr 25, 2012 at 5:15
  • 2
    Best link on SO for TimeZone and daylight saving time:- stackoverflow.com/questions/2532729/… Commented Apr 25, 2012 at 5:25

3 Answers 3

1

This is not a valid jquery statement

currentDate.setTimezone("Asia/Dubai");

just remove this line and you'll get the timezone offset.

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

1 Comment

hi my query is i need to convert asia/dubai to +4... please read the first line of the description
1

The Date object doesn't know a setTimezone method. If you want to set a Date to the 'Asia/Dubai' timezone, this could be a way:

var regionalDate = new Date;
regionalDate.setHours(regionalDate.getUTCHours()+4); //=> set to UTC+4

3 Comments

hi my query is i need to convert asia/dubai to +4..please read the first line of the description
Well, you'll have to think of something yourself for such a conversion. There's no miraculous native timezoneconverter in javascript. It could be as easy as assigning a value to a variable called 'Dubai' (var Dubai = 4), or creating an object for different timezones (var zones = {Asia: { Dubai:4, Brunei:8, Mumbai: 5.5 }, Europe: {Amsterdam: {summer:2; winter:1} }) etc. It's up to you. See also: timeanddate.com/worldclock
0

use locale "ia" with Intl.DateTimeFormat :

const getOffset = (tz) => Intl.DateTimeFormat("ia", {
                       timeZoneName: "shortOffset",
                       timeZone : tz
                     })
                    .formatToParts()
                    .find((i) => i.type === "timeZoneName").value // => "GMT+/-hh:mm"
                    .slice(3); //=> +/-hh:mm

console.log('Asia/Dubai', ' UTC' + getOffset('Asia/Dubai'))

// to get your user timezone :

const tz = Intl.DateTimeFormat().resolvedOptions().timeZone

console.log(tz + ' UTC' + getOffset(tz))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.