4

Possible Duplicate:
Getting the client’s timezone in JavaScript

Is there anyway to get current timezone in javascript. All i see is

Date.getTimeZoneOffset()

The problem I am facing is, I have a piece of functionality which allows the users to download pdf, now the pdf has to include a 'download date' in it which should be users local date, have to do this in java. But its all confusing, I dont know if i should locale or not or just the timezone will work, each time when i try something the results are different!!!!

7
  • 2
    That would be the current timezone... Commented Nov 13, 2012 at 17:15
  • 1
    Do you mean the time zone name, such as "CET" or "Europe/Madrid"? Commented Nov 13, 2012 at 17:15
  • bitbucket.org/pellepim/jstimezonedetect Commented Nov 13, 2012 at 17:15
  • 2
    @Shmiddty UTC offset is not a timezone, the offset changes depending on the computer's real timezone and the date object's value whereas a timezone like 'Europe/London' encapsulates the DST changes and offsets depending on time. Commented Nov 13, 2012 at 17:16
  • 3
    Please don't close this as a duplicate for that question without reading it through first; as the other question (and accepted answer) focus on the offset via getTimeZoneOffset which was mentioned in the question here .. Commented Nov 13, 2012 at 17:27

1 Answer 1

8

Use Date.toTimeString()

Code:

var date = new Date();
document.write(date.toTimeString());

Output:

22:53:21 GMT+0530 (India Standard Time)
              ^offset     ^timezone
Sign up to request clarification or add additional context in comments.

4 Comments

I get new Date( 2012, 0 ).toTimeString(); //"00:00:00 GMT+0200 (FLE Standard Time)" and new Date( 2012, 6 ).toTimeString(); //00:00:00 GMT+0300 (FLE Daylight Time) which are not that useful, when a simple Europe/Helsinki would have encapsulated all of this in a single timezone.
@Esailija In any case, that library you mentioned will serve better. I was just trying to find that piece of information using built-in functions.
The problem I am facing is, I have a piece of functionality which allows the users to download pdf, now the pdf has to include a 'download date' in it which should be users local date, have to do this in java. But its all confusing, I dont know if i should locale or not or just the timezone will work, each time when i try something the results are different!!!!
If you are looking for the date, JS won't really be reliable. Can't use the server-side date?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.