2

In a web page, using Javascript, I want to display the user's local timezone information nicely. For instance, if a user from California (Pacific time) browses the web page, I want to display something like this:

Pacific Standard Time

If the long time zone name is not available then at least I want to display 'PST' in this case.

Is there a standard way to do this in Javascript? I've looked around, but couldn't find a satisfactory solution. The Date object's toLocaleString method comes close, but it's not quite what I want.

1
  • Unfortunately, any of the below methods that pull out part of the response from .toString() will give you inconsistent results, because the implementations are slightly different in some browsers, especially older ones. Commented Mar 5, 2014 at 1:22

3 Answers 3

2

How about this one ? you can add some conditions around it but the basic is

var now = new Date();
var tz = now.toString().split("GMT")[1];
tz.substr(7,tz.length -8)

OR

var now = new Date(); 
var zone = /\(.*\)/.exec(now.toString())[0];
Sign up to request clarification or add additional context in comments.

Comments

1

What you are probably looking for is a simple code snippet. However, working with date formatting is an area that is greatly helped by using a good tool, and Moment.js is in my opinion, the best: http://momentjs.com/

Or, you can do this

var myDate = new Date(); 
var timeZone = /\(.*\)/.exec(myDate.toString())[1];

2 Comments

Moment.js is a great tool. Actually I had taken a look at its document too, but I couldn't find what I wanted. @Harvey, can you be more specific about Moment.js?
Jake, it's not the best tool to do exactly what you requested. That's why I amended my answer with a simple way to print the time zone. It's a tool that's designed to format dates, compare them, and to convert dates from one time zone to another.
0

I think you're looking fore something like:

console.log(new Date().toString());

For an easy to read list of Date Object methods see: http://www.w3schools.com/jsref/jsref_obj_date.asp .

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.