Jake
Jake

Reputation: 1580

How to display user's time zone information nicely in javascript

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.

Upvotes: 2

Views: 156

Answers (3)

Harvey A. Ramer
Harvey A. Ramer

Reputation: 782

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];

Upvotes: 1

Venkat Reddy
Venkat Reddy

Reputation: 1052

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];

Upvotes: 2

StackSlave
StackSlave

Reputation: 10617

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 .

Upvotes: 0

Related Questions