1

Currently I need to output a date in a: '5 October, 2012' type format. Meaning day-of-month with no leading zeros, space, full month name, comma, space, four-digit year. I need to do this in JavaScript. I have this working but it occurs to me while writing the lengthy code that somebody must've already figured out a better way to do this.

I don't think there is a built in function of JavaScript that formats this exactly how I want. There is just a thing in PHP with date(). Is there a plugin for JavaScript that does the same thing?

For the sake of giving a specific example, in this instance I start with a set number of hours into the future that I need to get the date for.

Currently I have:

    var myNow = new Date().getTime();
    var myTime = hours * 60 * 60 * 1000;
    var myDate = new Date(myTime + myNow);
    var myDay = myDate.getDate();
    var myMonthNum = myDate.getMonth();
    var myMonth = '';
    var myYear = myDate.getFullYear();

    switch(myMonthNum) {
        case 0:
            myMonth = 'January';
            break;
        ...

    var completeDate = myDate = " " + myMonth + ", " + myYear;
    $('#theEndDate').html(completeDate);
Surreal Dreams
  • 26,055
  • 3
  • 46
  • 61
Peter Oram
  • 6,213
  • 2
  • 27
  • 40
  • 2
    DateJS :) http://www.datejs.com – Andrew Whitaker Sep 28 '12 at 00:53
  • 1
    DateJS hasn't been updated in almost 5 years.. I'd personally use [momentJS](http://momentjs.com/) but if you're doing something as simple as that, there shouldn't be any need to load an external library when your short code does exactly what you want. – Fabrício Matté Sep 28 '12 at 01:02
  • 1
    Since you know some PHP, check out php.js at phpjs.org. Here's their JS equivalent of date(): http://phpjs.org/functions/date:380 The site is a good resource for those who know some PHP and struggle to find similar functions in JS. – Surreal Dreams Sep 28 '12 at 01:06

3 Answers3

3

No, there is nothing built-in for Dateobjects, but there are a bunch of libraries to deal with and format them:

Basj
  • 41,386
  • 99
  • 383
  • 673
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

You've got some extraneous code that you can clean up:

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
    today = new Date(),
    completeDate = today.getDate() + " " + months[today.getMonth()] + ", " + today.getFullYear();

$('#theEndDate').html(completeDate);

Using a library isn't always the answer, especially if you are only going to use it in one spot.

Dennis
  • 32,200
  • 11
  • 64
  • 79
-1

There are lots of date libraries, but if all you want to do is generate one specific format from a date object, it's pretty trivial:

var formatDate = (function() {

  var months = ['January','February','March','April','May','June',
      'July','August','September','October','November','December'];

  return function(date) {
    return date.getDate() + ' ' + months[date.getMonth()] + ', ' + date.getFullYear();
  }
}());

alert(formatDate(new Date())); // 28 September, 2012

I think that took less than 5 minutes.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • It is obviously trivial. It was never a question of difficulty or even handling that one specific scenario. More a point of having to lay it all out every time any date handling is needed gets a bit unwieldy. – Peter Oram Oct 03 '12 at 21:53
  • Generally you only want one format for each application, writing a function is pretty simple and can become part of your toolbox. You'll likely spend less time doing that than looking for and evaluating date libraries. – RobG Oct 03 '12 at 23:53