5

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

akshay bagade
  • 1,169
  • 1
  • 11
  • 24
  • 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 Sep 05 '19 at 12:44
  • 1
    I want to convert this DateTime to offset 260, currently it is in offset 059 – akshay bagade Sep 05 '19 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 company, 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 Sep 05 '19 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 Sep 05 '19 at 13:00

3 Answers3

7

You can convert to prefered time zone by following way ,

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
Pushprajsinh Chudasama
  • 7,772
  • 4
  • 20
  • 43
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>
ashish bandiwar
  • 378
  • 1
  • 3
  • 12
1

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

HungrySoul
  • 1,151
  • 2
  • 17
  • 31