2016-12-05 4 views
0

Ich verwende eine Schaltfläche, um ein Formularfeld mit Datum und Zeitstempel zu aktualisieren. Das Problem besteht nun darin, dass die Anfrage so erfolgt ist, dass sie bei jeder Verwendung in die zentrale Zeitzone aktualisiert werden. Kann jemand mir helfen, das unten zu aktualisieren, damit ich mich unterbringen kann?JavaScript Get Time Central Zeitzone

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

Antwort

0

Von 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

Check out moment.js, und sein Komplement moment-timezone.js:

http://momentjs.com

http://momentjs.com/timezone

Zum Beispiel wird dies ou Geben Sie die aktuelle Uhrzeit in die zentrale Zeitzone ein:

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