Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. var serverDate = '2014-02-24 04:52:48.050000'; // EST
  2.  
  3. function convertServerDateToLocal(dateInput) {
  4. // EST - UTC offset: 5 hours
  5. var offset = 5.0,
  6.  
  7. /*
  8. - calculate the difference between the server date and UTC
  9. - the value returned by the getTime method is the number of milliseconds since 1 January 1970 00:00:00 UTC.
  10. - the time-zone offset is the difference, in minutes, between UTC and local time
  11. - 60000 milliseconds = 60 seconds = 1 minute
  12. */
  13. serverDate = new Date(dateInput),
  14. utc = serverDate.getTime() - (serverDate.getTimezoneOffset() * 60000),
  15.  
  16. /*
  17. - apply the offset between UTC and EST (5 hours)
  18. - 3600000 milliseconds = 3600 seconds = 60 minutes = 1 hour
  19. */
  20. clientDate = new Date(utc + (3600000 * offset));
  21.  
  22. return clientDate.toLocaleString();
  23. }
  24.  
  25. console.log(convertServerDateToLocal(serverDate));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement