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?
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?
This is an universal solution:
Usage:
Display the date based on the client local setting:
I Answering This If Any one want function that display converted time to specific id element and apply date format string yyyy-mm-dd here date1 is string and ids is id of element that time going to display.
i know that answer has been already accepted but i get here cause of google and i did solve with getting inspiration from accepted answer so i did want to just share it if someone need.
Use this for UTC and Local time convert and vice versa.
Based on @digitalbath answer, here is a small function to grab the UTC timestamp and display the local time in a given DOM element (using jQuery for this last part):
https://jsfiddle.net/moriz/6ktb4sv8/1/
This works for me:
In case you don't mind using
moment.js
and your time is in UTC just use the following:if your time is not in utc but any other locale known to you, then use following:
if your time is already in local, then use following: