Created
November 1, 2014 21:30
-
-
Save partageit/c0d0cc4a8537b32f2663 to your computer and use it in GitHub Desktop.
Translate GMT date to browser's timezone, with locale format
This file contains 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
/** | |
* Get a date as 'YYYY-MM-DD HH:MM:SS' as GMT, and returns the same date according to the browser's timezone, i.e. GMT+n. | |
* The returned date is a string, with the locale format. | |
* @param string dateString The date as 'YYYY-MM-DD HH:MM:SS' | |
* @param bool withHour If false (default), the 'hour' part is excluded from the returned string | |
* @return string The date/time according to the browser's locale timezone and format. | |
* @example gmtToLocaleDate('2014-10-31 21:52:17', true) returns '31/10/2014 21:52:17' (according to my French browser locale, as october is GMT+1) | |
* @example gmtToLocaleDate('2014-05-31 21:52:17', true) returns '31/05/2014 23:52:17' (as May is GMT+2) | |
*/ | |
var gmtToLocaleDate = function(dateString, withHour) { | |
if (!dateString) return ''; | |
var d = new Date(dateString.split(' ').join('T')); | |
d.setMinutes(d.getMinutes() - d.getTimezoneOffset()) | |
return (withHour ? d.toLocaleString() : d.toLocaleDateString()); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment