Skip to content

Instantly share code, notes, and snippets.

@mihaipaun
Last active August 20, 2021 07:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mihaipaun/9191226 to your computer and use it in GitHub Desktop.
Save mihaipaun/9191226 to your computer and use it in GitHub Desktop.
JavaScript: Convert from EST to user's local time zone
var serverDate = '2014-02-24 04:52:48.050000'; // EST
function convertServerDateToLocal(dateInput) {
// EST - UTC offset: 5 hours
var offset = 5.0,
/*
- calculate the difference between the server date and UTC
- the value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.
- the time-zone offset is the difference, in minutes, between UTC and local time
- 60000 milliseconds = 60 seconds = 1 minute
*/
serverDate = new Date(dateInput),
utc = serverDate.getTime() - (serverDate.getTimezoneOffset() * 60000),
/*
- apply the offset between UTC and EST (5 hours)
- 3600000 milliseconds = 3600 seconds = 60 minutes = 1 hour
*/
clientDate = new Date(utc + (3600000 * offset));
return clientDate.toLocaleString();
}
console.log(convertServerDateToLocal(serverDate));
@mamoorkhan
Copy link

mamoorkhan commented Nov 9, 2016

does this code consider daylight savings?

@jlsicajan
Copy link

does this code consider daylight savings?

no :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment