最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - convert datetime from one offset to another offset - Stack Overflow

programmeradmin3浏览0评论

How to convert 2019-09-05T11:31:34.059Z this DateTime to offset 260.

database:mssql, datatype:datetimeoffset(7).

As one can identify current time-offset then how to convert the given date to this time-offset

How to convert 2019-09-05T11:31:34.059Z this DateTime to offset 260.

database:mssql, datatype:datetimeoffset(7).

As one can identify current time-offset then how to convert the given date to this time-offset

Share Improve this question asked Sep 5, 2019 at 12:35 akshay bagadeakshay bagade 1,2191 gold badge12 silver badges24 bronze badges 4
  • 1 Just new Date( '2019-09-05T11:31:34.059Z' );, since it's in the correct format. Most of the methods of the date object mention if they give local time or UTC time. So getHours() would give you local hours, getUTCHours() would give you UTC hours. Using toLocaleString() you can get a local timezone string, as shown below. And basic toString() will give you whatever is the standard in your local time. – Shilly Commented Sep 5, 2019 at 12:44
  • 1 I want to convert this DateTime to offset 260, currently it is in offset 059 – akshay bagade Commented Sep 5, 2019 at 12:52
  • 2019-09-05T11:31:34.059Z is the ISO8601 standard notation for datetimes. It should be the UTC time. But let me guess, just as in my pany, the database admins did not think this through when creating the database and provide you a UTC timestamp that is not UTC? The correct action would be to fix the database, since everyone using it will have this problem. – Shilly Commented Sep 5, 2019 at 12:56
  • just stored new Date() to mssql database of datatype datetimeoffset(7), and query for it then it return above datetime – akshay bagade Commented Sep 5, 2019 at 13:00
Add a ment  | 

3 Answers 3

Reset to default 7

You can convert to prefered time zone by following way ,

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
<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>

If you are convenient with 3rd party libraries, you can go with moment.js along with moment-tz (moment with timezone).

发布评论

评论列表(0)

  1. 暂无评论