0

I've a requirement where I have to show the Date timestamp in following format.

MM:DD:YYYY HH:MM:SS IST/CST/CDT.

I have used javascript date object to get the date and time. But I don't know how to get the timezone (IST or CST or CDT or etc) from the obj. Do we have any technique or native javascript Plugin (non-jQuery plugin) to get this timezone abbreviated value?

2 Answers 2

1

Check out moment.js library and its format method, with which you can format a date to include the time zone.

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

1 Comment

You can use momentjs and jstimezone github.com/raulferras/jstimezone , but I suggest reading this excellent article too: joshowens.me/dealing-with-timezones-in-javascript
1

Below is pure JS code, without using any third party library

var x = new Date();

var tz = x.toTimeString().match(/\((.+)\)/)[1];

var month = x.getMonth() + 1;
month = month < 10 ? ('0' + month) : month.toString();

var date = x.getDate() < 10 ? ('0' + x.getDate()) : x.getDate().toString();

var hour = x.getHours() < 10 ? ('0' + x.getHours()) : x.getHours().toString();

var min = x.getMinutes() < 10 ? ('0' + x.getMinutes()) : x.getMinutes().toString();

var sec = x. getSeconds() < 10 ? ('0' + x.getSeconds()) : x.getSeconds().toString();

var output = month + ':' + date + ':' + x.getFullYear() + ' ' + hour + ':' + min + ':' + sec + ' ' + tz

6 Comments

this is what I coded. Lately I found the Date object will give diff kind of string in IE. This code works perfectly in Chrome.
Which IE version are you using??
It should work. What output are you getting for toTimestring()?
Output from chrome: "19:39:25 GMT+0530 (India Standard Time)" Output from IE11: "19:40:08 UTC+0530"
19:42:47 GMT+0530 (IST). this is the output I get for toTimeString() on chrome. and the full code gives "11:17:2015 19:42:16 IST"
|

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.