4

I need to format the JavaScript Date object. It should display date and time in short format with regard to the culture that browser is using, for example:

20/10/2011 10:20 PM
10.20.2011 22:20

How do I do this (preferably with jQuery)?

Liam
  • 27,717
  • 28
  • 128
  • 190
IT Hit WebDAV
  • 5,652
  • 12
  • 61
  • 98
  • Preferably with jQuery? Why is that preferable? – Jared Farrish Dec 12 '11 at 02:56
  • 1
    Also, I think you're looking for the [`toLocale` methods](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date). – Jared Farrish Dec 12 '11 at 02:58
  • There's a jquery plugin for that. But it has a dependency upon the jquery basic arithmetic plugin. – goat Dec 12 '11 at 03:36
  • @JaredFarrish - *Date.prototype.toLocaleString()* is inconsistent across browsers and can't be used to reliably present dates based on local system settings. – RobG Dec 12 '11 at 04:35
  • @chris so what is the name of the plugin? Could you give an example of usage? – IT Hit WebDAV Dec 14 '11 at 00:20
  • @Jared Farrish how to use the toLocale method to output date in a short date format? Could you give an example? – IT Hit WebDAV Dec 14 '11 at 00:26
  • @user695797 - you can't use *toLocaleString()* for that, at least not easily, because it is implementation dependant. That is, you will get something different in each browser, and most don't give a short form (see my answer). – RobG Dec 14 '11 at 05:57

3 Answers3

4

Browsers either use system settings for date formats, or use their own (often US–centric) settings.

There is a Date.prototype.toLocaleDateString() method that should return a date based on current system settings, however it's implementation dependent and completely unreliable due to being inconsistent between browsers.

e.g. for me on 13 December, 2011:

  1. Safari returns 13 December 2001
  2. Firefox 12/13/2011
  3. Opera Tuesday December 13, 2011
  4. Chrome Tuesday, December 13, 2011
  5. IE 6 Tuesday, 13 December, 2011

So only Safari and IE actually use the system settings, it seems the developers of other browsers are either too lazy, indifferent or ignorant to accommodate non–US users.

An alternative is to either ask the user what format they prefer, or just use an unambiguous format, e.g. 13-Dec-2011 will be understood by everyone. If you really must use numbers only, then the ISO-8601 format should do fine: 2011-12-13 with the added benefit that it is simple to sort.

Some functions that print a short date in the above formats:

// format: 2011/12/5
function shortDate1(obj) {
  obj = obj || new Date();
  return obj.getFullYear() + '/' + (obj.getMonth() + 1) + '/' + obj.getDate();
}


// format: 2011/12/05 
// (padded single digit month and day)
function shortDate2(obj) {

  function z(n) {
    return (n < 10? '0' : '') + n;
  }

  obj = obj || new Date();
  return obj.getFullYear() + '/' + z(obj.getMonth() + 1) + '/' + z(obj.getDate());
}


// format: 15-Dec-2011
function shortDate3(obj) {

  obj = obj || new Date();

  function z(n) {
    return (n < 10? '0' : '') + n;
  }

  months = ['Jan','Feb','Mar','Apr','May','Jun',
            'Jul','Aug','Sep','Oct','Nov','Dec'];

  return [z(obj.getDate()),months[obj.getMonth()],obj.getFullYear()].join('-');
}
RobG
  • 142,382
  • 31
  • 172
  • 209
  • 1
    And Safari return 2001 ? instead of 2011 ? – Anuj Pandey Feb 25 '12 at 05:59
  • I assume browsers differences apply only when a locale and/or other options are not explicitly set for `toLocaleDateString`?! Shouldn't output of eg. `str.toLocaleDateString('en-US', { year: 'numeric', month: 'long', day: 'numeric' })` be consistent across browsers?! – John Slegers Feb 26 '16 at 10:21
  • "ISO-8601 format should do fine: 2011/12/13" - that is not the format specified in ISO 8601. The valid formats in ISO 8601 are either "20111213" (basic format, digits only), or "2011-12-13" (extended format, it must be a dash). – Sly Gryphon May 30 '16 at 04:01
  • @SlyGryphon—thanks. This answer is from a long time ago… ;-) – RobG May 30 '16 at 05:15
3

The Date() object will get you the client time (live demo here):

var now=new Date();
alert(now.toLocaleString());

JS and jQuery don't have a built in format function. To format it differently use a function like format() (1.2kb) here and then the following code will produce a format like "10/10/2012 8:50pm" :

var now=new Date();    
alert( now.format("mm/dd/yy h:MM:ss TT") );
themerlinproject
  • 3,542
  • 5
  • 37
  • 55
  • P.S. You could always echo or print the time into your JS using your server-side language (e.g. PHP) as those languages generally have much more robust time formatting options – themerlinproject Dec 12 '11 at 03:12
  • Where in the above is there an answer to the OP? Formatting a date is quite simple, there really is no need for a library to do it. Presenting dates in a format consistent with system settings in a reliable, cross–browser function is impossible. – RobG Dec 12 '11 at 04:33
  • As far as I see none of this can output '20/10/2011' or '10.20.2011' depending on browser culture. – IT Hit WebDAV Dec 12 '11 at 05:04
  • @user695797 and Rob- when the question was first published it did not ask for that specific date format (e.g. 20/10/2011) and only later added that. – themerlinproject Dec 12 '11 at 05:13
  • @user695797 - with the function I referenced (please visit link) you can create any format you wish, including the one you ask for: format("mm/dd/yy h:MM:ss TT") will do the trick (answer updated). – themerlinproject Dec 12 '11 at 05:17
  • @themerlinproject I do not need the library to print the date. I need the library that can output it with regard to browser locale/culture in a short format. I have clearly stated this in a question even before the editing. I have edited the formatting later. – IT Hit WebDAV Dec 14 '11 at 00:22
  • @themerlinproject no, the now.format("mm/dd/yy h:MM:ss TT") would not produce what I asked in the question. – IT Hit WebDAV Dec 14 '11 at 00:47
  • @user695797 - you can change the formatting around to format it however you want, that is what the library does. – themerlinproject Dec 14 '11 at 07:04
3

It's handy to know if a system uses day-month or month-day in its string methods, mostly to set the order of user inputs. toLocaleString is fine for displaying a known date- and it is international.

    Date.dmOrder=(function(){
      return Date.parse('2/6/2009')> Date.parse('6/2/2009');
    })()

if(Date.dmOrder)// ask for the date first
else // ask for the month first
kennebec
  • 102,654
  • 32
  • 106
  • 127
  • toLocaleString is not fine, nor is it international. In most browsers (Chrome, Firefox, IE, Opera at least) it is very much US-centric. – RobG Dec 14 '11 at 02:40
  • 1
    and how do I get the delimiter format? Some cultures use '/' and some '.', I expect there are other culture-specific delimiters. – IT Hit WebDAV Dec 16 '11 at 18:43