2016-12-05 37 views
0

我正在使用按鈕來更新帶有日期和時間戳的表單域。現在的問題是請求已經完成,以便隨時使用這些請求,它們將被更新到中央時區。任何人都可以幫助我更新下面的內容,以便我可以適應?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(); 
} 

回答

0

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

退房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