2

我将日期保存在 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;

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

4

1 回答 1

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

}

此函数通过提供城市/国家名称和偏移值来计算时区值很有用

于 2014-06-16T12:33:38.207 回答