JavaScript获取时间中心时区

时间:2016-12-05 21:27:09

标签: javascript

我正在使用按钮更新带有日期和时间戳的表单字段。现在的问题是已经发出请求,以便在使用这些请求时,它们将被更新到中央时区。任何人都可以帮我更新以下内容,以便我可以容纳吗?

function getTimeStamp() {
  var now = new Date();
  return ((now.getMonth() + 1) + '/' + (now.getDate()) + '/' + now.getFullYear() + " " + now.getHours() + ':' +
    ((now.getMinutes() < 10) ? ("0" + now.getMinutes()) : (now.getMinutes())) + ':' + ((now.getSeconds() < 10) ? ("0" + now
      .getSeconds()) : (now.getSeconds())));
}

function setTime() {
  document.getElementById('InsertRecordDate_Received').value = getTimeStamp();
}

2 个答案:

答案 0 :(得分:2)

结帐moment.js及其补充moment-timezone.js

http://momentjs.com

http://momentjs.com/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

答案 1 :(得分:1)

来自http://www.techrepublic.com/article/convert-the-local-time-to-another-time-zone-with-this-javascript/6016329

/** 
 * 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();
}