Last active
August 20, 2021 07:46
-
-
Save mihaipaun/9191226 to your computer and use it in GitHub Desktop.
JavaScript: Convert from EST to user's local time zone
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
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
does this code consider daylight savings?