Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert datetime from one offset to another 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

like image 289
akshay bagade Avatar asked Sep 05 '19 12:09

akshay bagade


People also ask

How do you change date time offset of a given date?

You can also use the DateTimeOffset. LocalDateTime property to convert a DateTimeOffset value to a local DateTime value. The Kind property of the returned DateTime value is Local.

How do you change from one time zone to another?

Changing Timezones of ZonedDateTime To convert a ZonedDateTime instance from one timezone to another, follow the two steps: Create ZonedDateTime in 1st timezone. You may already have it in your application. Convert the first ZonedDateTime in second timezone using withZoneSameInstant() method.

How do I convert one time zone to another in C#?

ConvertTime(DateTimeOffset, TimeZoneInfo) method that performs time zone conversions with ToOffset(TimeSpan) values. The method's parameters are a DateTimeOffset value and a reference to the time zone to which the time is to be converted. The method call returns a DateTimeOffset value.

How do I convert DateTimeOffset to local time?

In performing the conversion to local time, the method first converts the current DateTimeOffset object's date and time to Coordinated Universal Time (UTC) by subtracting the offset from the time. It then converts the UTC date and time to local time by adding the local time zone offset.


3 Answers

You can convert to prefered time zone by following way ,

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
like image 68
Pushprajsinh Chudasama Avatar answered Oct 26 '22 02:10

Pushprajsinh Chudasama


<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>
like image 20
ashish bandiwar Avatar answered Oct 26 '22 04:10

ashish bandiwar


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

like image 1
HungrySoul Avatar answered Oct 26 '22 02:10

HungrySoul