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:34 CST
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
*/
function calcTime(city, offset) {
// create Date object for current location
var d = new Date();
// convert to msec
// add local time zone offset
// get UTC time in msec
var utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object for different city
// using supplied offset
var nd = new Date(utc + (3600000*offset));
// return time as a string
return "The local time in " + city + " is " + nd.toLocaleString();
}
Post a Comment for "JavaScript Get Time Central Time Zone"