如何在具有不同时区的javascript中更改日期

时间:2014-06-16 12:26:59

标签: javascript php date datetime timestamp-with-timezone

我在unix-timestamp中保存数据库中的日期。我已将时区默认设置为:

date_default_timezone_set("America/Los_Angeles");

但是在javascript中我以下列方式更改时间戳:

for (var i = 0; i < records.length; i++) {
    if (originalData[i].SystemLogsUserAction.TimeStamp == "0") {
        records[i].TimeStamp = "";
    } else {
        records[i].TimeStamp = new Date(originalData[i].SystemLogsUserAction.TimeStamp * 1000);
    }
}
return records;

上面的代码改变了时间,但不是我提到的时区。

1 个答案:

答案 0 :(得分:4)

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
    d = new Date();

    // convert to msec
    // add local time zone offset
    // get UTC time in msec
    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

    // create new Date object for different city
    // using supplied offset
    nd = new Date(utc + (3600000*offset));

    // return time as a string
    return "The local time in " + city + " is " + nd.toLocaleString();

}

此功能可用于通过提供城市/国家/地区的名称和偏移值来计算时区值