将日期时间从一个偏移量转换为另一个偏移量

时间:2019-09-05 12:35:48

标签: javascript node.js reactjs react-native datetimeoffset

如何将2019-09-05T11:31:34.059Z这个DateTime转换为偏移量260。

数据库:mssql, 数据类型:datetimeoffset(7)。

由于可以识别当前时间偏移,然后如何将给定日期转换为此时间偏移

3 个答案:

答案 0 :(得分:6)

您可以通过以下方式转换为首选时区,

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})

答案 1 :(得分:2)

<html>
<head>
<script language="JavaScript">

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

}

// get Bombay time
alert(calcTime('Bombay', '+5.5'));

// get Singapore time
alert(calcTime('Singapore', '+8'));

// get London time
alert(calcTime('London', '+1'));

</script>
</head>
<body>

</body>
</html>

答案 2 :(得分:1)

如果方便使用第三方库,则可以将moment.js和moment-tz(带时区的时刻)一起使用。