Javascript Get Time Central Time Zone
I am using a button to update a form field with a date and timestamp. The issue now is that the request has been made so that anytime these are used they are being updated to the
Solution 1:
Check out moment.js
, and its complement moment-timezone.js
:
For example, this will output the current time converted to central timezone:
moment().tz('America/Chicago').format('hh:mm:ss z')
> 03:48:34CST
moment().tz('America/Chicago').format('hh:mm:ss z Z')
> 03:50:35 CST -06:00
moment().tz('America/Chicago').format()
> 2016-12-05T15:52:09-06:00
Solution 2:
/**
* function to calculate local time
* in a different city
* given the city's UTC offset
*/functioncalcTime(city, offset) {
// create Date object for current locationvar d = newDate();
// convert to msec// add local time zone offset// get UTC time in msecvar utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city// using supplied offsetvar nd = newDate(utc + (3600000*offset));
// return time as a stringreturn"The local time in " + city + " is " + nd.toLocaleString();
}
Post a Comment for "Javascript Get Time Central Time Zone"