Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert UTC date time to local date time

From the server I get a datetime variable in this format: 6/29/2011 4:52:48 PM and it is in UTC time. I want to convert it to the current user’s browser time zone using JavaScript.

How this can be done using JavaScript or jQuery?

like image 717
Amr Elgarhy Avatar asked Jun 29 '11 18:06

Amr Elgarhy


People also ask

How do I convert UTC to local date?

(GMT-5:00) Eastern Time (US & Canada)Add the local time offset to the UTC time. For example, if your local time offset is -5:00, and if the UTC time is shown as 11:00, add -5 to 11. The time setting when adjusted for offset is 06:00 (6:00 A.M.).

How do I convert a UTC date to a local date in typescript?

Use the Date() constructor to convert UTC to local time, e.g. new Date(utcDateStr) . Passing a date and time string in ISO 8601 format to the Date() constructor converts the UTC date and time to local time.

How do I convert UTC to local date in Excel?

=TIMEVALUE(RIGHT(A1,5))+DATEVALUE(SUBSTITUTE(LEFT(A1,10),".","/"))+TIME(8,0,0) in B1.

How do I convert datetime to local time?

The ToLocalTime method converts a DateTime value from UTC to local time. To convert the time in any designated time zone to local time, use the TimeZoneInfo. ConvertTime method. The value returned by the conversion is a DateTime whose Kind property always returns Local.


1 Answers

Append 'UTC' to the string before converting it to a date in javascript:

var date = new Date('6/29/2011 4:52:48 PM UTC'); date.toString() // "Wed Jun 29 2011 09:52:48 GMT-0700 (PDT)" 
like image 91
digitalbath Avatar answered Oct 14 '22 08:10

digitalbath