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
Other answers
Andrew Lewis
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:
- How to Convert Json date string to more readable date format?Best solution by SharePoint
- How to uninstall Firefox add-on using javascript?Best solution by Stack Overflow
- How to store global date time and convert it to local date time?Best solution by Stack Overflow
- How to convert full date/time to timestamp?Best solution by Stack Overflow
- How do we convert past date into future date?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.