Skip to content

Instantly share code, notes, and snippets.

@noromanba
Last active December 24, 2015 03:19
Show Gist options
  • Save noromanba/6736822 to your computer and use it in GitHub Desktop.
Save noromanba/6736822 to your computer and use it in GitHub Desktop.

Cross Browser date-format check

c.f.

code

var now = new Date();

now.getFullYear()
    + '-'  + ('0' + (now.getMonth() + 1)).slice(-2)
    + '-'  + ('0' + now.getDate()).slice(-2)
    + ' '  + now.toLocaleTimeString('en-GB');
// => "2013-09-06 10:02:22"

now.getFullYear()
    + '-' + ('0' + (now.getMonth() + 1)).slice(-2)
    + '-' + ('0' + now.getDate()).slice(-2)
    + ' ' + now.toTimeString();
// => "2013-09-06 10:02:22 GMT+0900 (JST)"

now.toLocaleString('en', {
    year: 'numeric', month:  '2-digit', day:    '2-digit',
    hour: '2-digit', minute: '2-digit', second: '2-digit',
    timeZoneName: 'short'
});
// => "09/28/2013 10:02:22 AM Japan Time"

now.toLocaleString('en-US', {
    year: 'numeric', month:  '2-digit', day:    '2-digit',
    hour: '2-digit', minute: '2-digit', second: '2-digit',
    timeZoneName: 'short'
});
// => "09/28/2013 10:02:22 AM Japan Time"

now.toLocaleString('en-GB', {
    year: 'numeric', month:  '2-digit', day:    '2-digit',
    hour: '2-digit', minute: '2-digit', second: '2-digit',
    timeZoneName: 'short'
});
// => "28/09/2013 10:02:22 Japan Time"

now.toLocaleString('ja', {
    year: 'numeric', month:  '2-digit', day:    '2-digit',
    hour: '2-digit', minute: '2-digit', second: '2-digit',
    timeZoneName: 'short'
});
// => "2013/09/28 10時02分22秒 JST"

Environment

Google Chrome   29.0.1547.76 (Official Build 223446)
OS              Linux
Blink           537.36 (@156722)
JavaScript      V8 3.19.18.23
Flash           11.8.800.170
User Agent      Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.76 Safari/537.36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment