I have a date in the format of ISO string
date = new Date ().toISOString()
which returns me "2015-12-08T14:03:36.129Z"
now i want to change this date to CST and want output like
December 23, 2015 11: 30 AM
I have a date in the format of ISO string
date = new Date ().toISOString()
which returns me "2015-12-08T14:03:36.129Z"
now i want to change this date to CST and want output like
December 23, 2015 11: 30 AM
Note that CST is a single time zone that is UTC-0600. If you want daylight saving in the same zone, you'll need to program that yourself.
To get the time in any time zone, you can adjust the date's UTC time for the required offset, then just read the UTC values, like the following:
function getCST(date) {
var months = ['January','February','March','April','May','June','July',
'August','September','October','November','December'];
var d = new Date(+date);
// CST is UTC -0600 so subtract 6 hours and use UTC values
d.setUTCHours(d.getUTCHours() - 6);
return months[d.getUTCMonth()] + ' ' +
d.getUTCDate() + ', ' +
d.getUTCFullYear() + ' ' +
((d.getUTCHours()%12) || 12) + ':' +
('0' + d.getUTCMinutes()).slice(-2) + ' ' +
(d.getUTCHours() < 12? 'AM':'PM');
}
document.write("Current CST time is: " + getCST(new Date()));
You can just format it yourself (assuming CST = north american central standard time = UTC - 6 hours)
function getCSTDate(){
var months = ["January","February","March","April","May","June",
"July","August","September","October","November","December"];
var date = new Date();
var meridian;
var cstdate = "";
date = new Date(
date.getUTCFullYear(),
date.getUTCMonth(),
date.getUTCDate(),
date.getUTCHours() - 6,
date.getUTCMinutes(),
date.getUTCSeconds(),
date.getUTCMilliseconds()
);
meridian = (date.getHours() < 12)?"AM":"PM";
//CST date example: December 23, 2015 11:30 AM
cstDate = months[date.getMonth()]
+ " "
+ date.getDate()
+ ", "
+ date.getFullYear()
+ " "
+ date.getHours()
+ ":"
+ date.getMinutes()
+ " "
+ meridian;
return cstDate;
}
Obvious Problem with this approach: daylight saving time. You can do it by hand (doesn't change very often) or use something like TimezoneJS or MomentJS-timezone.