How to convert UTC to local time in Javascript?

Convert UTC date time to local date time using JavaScript

  • 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 using JavaScript. How this can be done using JavaScript or jQuery?

  • Answer:

    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)"

Amr ElGarhy at Stack Overflow Visit the source

Was this solution helpful to you?

Other answers

You should get the (UTC) offset (in minutes) of the client: var offset = new Date().getTimezoneOffset(); And then do the correspondent adding or substraction to the time you get from the server. Hope this helps.

Nobita

Put this function in your head: <script type="text/javascript"> function localize(t) { var d=new Date(t+" UTC"); document.write(d.toString()); } </script> Then generate the following for each date in the body of your page: <script type="text/javascript">localize("6/29/2011 4:52:48 PM");</script> To remove the GMT and time zone, change the following line: document.write(d.toString().replace(/GMT.*/g,""));

Ben Bryant

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.