admin管理员组文章数量:1356548
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. SogetHours()
would give you local hours,getUTCHours()
would give you UTC hours. UsingtoLocaleString()
you can get a local timezone string, as shown below. And basictoString()
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
3 Answers
Reset to default 7You 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).
本文标签: javascriptconvert datetime from one offset to another offsetStack Overflow
版权声明:本文标题:javascript - convert datetime from one offset to another offset - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743970462a2570650.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论