Francesco
Francesco

Reputation: 25239

Javascript: how to format a given datetime?

given my data is:

2011-12-31 01:00:00

what easy and quick script can I use to exctract simply: "DEC 31" ?

Upvotes: 2

Views: 273

Answers (5)

gilly3
gilly3

Reputation: 91497

A function that would do exactly what you asked for (and nothing more):

function toMonthAndDay(dateString) {
    var months = ['JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
    var dateParts = dateString.split(/[- ]/);
    return months[+dateParts[1]] + " " + dateParts[2];
}

But, to take any date and output it in any custom format, I wrote a function that is loosely based on .Net DateTime format strings:

Date.prototype.format = function (format)
{
    var MMMM = ["\u0000", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
    var MMM = ["\u0001", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
    var dddd = ["\u0002", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
    var ddd = ["\u0003", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
    function ii(i, len) { var s = i + ""; len = len || 2; while (s.length < len) s = "0" + s; return s; }

    var y = this.getFullYear();
    format = format.replace(/yyyy+/g, y);
    format = format.replace(/yy/g, y.toString().substr(2, 2));
    format = format.replace(/y/g, y);

    var M = this.getMonth() + 1;
    format = format.replace(/MMMM+/g, MMMM[0]);
    format = format.replace(/MMM/g, MMM[0]);
    format = format.replace(/MM/g, ii(M));
    format = format.replace(/M/g, M);

    var d = this.getDate();
    format = format.replace(/dddd+/g, dddd[0]);
    format = format.replace(/ddd/g, ddd[0]);
    format = format.replace(/dd/g, ii(d));
    format = format.replace(/d/g, d);

    var H = this.getHours();
    format = format.replace(/HH+/g, ii(H));
    format = format.replace(/H/g, H);

    var h = H > 12 ? H - 12 : H == 0 ? 12 : H;
    format = format.replace(/hh+/g, ii(h));
    format = format.replace(/h/g, h);

    var m = this.getMinutes();
    format = format.replace(/mm+/g, ii(m));
    format = format.replace(/m/g, m);

    var s = this.getSeconds();
    format = format.replace(/ss+/g, ii(s));
    format = format.replace(/s/g, s);

    var f = this.getMilliseconds();
    format = format.replace(/fff+/g, ii(f, 3));
    f = Math.round(f / 10);
    format = format.replace(/ff/g, ii(f));
    f = Math.round(f / 10);
    format = format.replace(/f/g, f);

    var T = H < 12 ? "AM" : "PM";
    format = format.replace(/TT+/g, T);
    format = format.replace(/T/g, T.charAt(0));

    var t = T.toLowerCase();
    format = format.replace(/tt+/g, t);
    format = format.replace(/t/g, t.charAt(0));

    var day = this.getDay() + 1;
    format = format.replace(new RegExp(dddd[0], "g"), dddd[day]);
    format = format.replace(new RegExp(ddd[0], "g"), ddd[day]);

    format = format.replace(new RegExp(MMMM[0], "g"), MMMM[M]);
    format = format.replace(new RegExp(MMM[0], "g"), MMM[M]);

    return format;
};

Usage:

new Date("2011-12-31 01:00:00".replace(/-/g, "/")).format("MMM d"); // returns Dec 31

Note that IE doesn't recognize "2011-12-31 01:00:00" as a valid date string. You have to replace the dashes with slashes. To get DEC instead of Dec, you'd have to call .toUpperCase().

The differences from .Net custom date format strings are:

  • You can make AM/PM uppercase or lowercase by using TT or tt respectively
  • Using \ as an escape character is not (yet) implemented.

Upvotes: 0

Gourneau
Gourneau

Reputation: 12868

http://www.datejs.com/ is nice for this

Using it the code would be like (tested and works)

Date.parse('2011-12-31 01:00:00').toString("MMM d"); // "Dec 31"

This solution is wonderful because datajs is a very flexible library.

Upvotes: 4

Wayne
Wayne

Reputation: 60414

Create the following helper functions:

function getMonthName(d) {
    var m = ['January','February','March','April','May','June','July',
        'August','September','October','November','December'];
    return m[d.getMonth()];
}

function getShortMonthName(d) {
    return getMonthName(d).substring(0, 3).toUpperCase();
}

And use them like this:

var s = "2011-12-31 01:00:00".split(/-|\s+|:/);
// new Date(year, month, day [, hour, minute, second, millisecond ])
var d = new Date(s[0], s[1] - 1, s[2], s[3], s[4], s[5]);

getShortMonthName(d) + " " + d.getDate();

Output:

"DEC 31"

Upvotes: 6

Joseph Marikle
Joseph Marikle

Reputation: 78520

This can do it. Just pass the string as a parameter to the date object and split the dateString. Concatenate and you're done :)

var n = new Date("2011-12-31 01:00:00");
var d = n.toDateString().split(" ");
var formattedDate = d[1].toUpperCase() + " " + d[2];

or optionally as a function

function getFormattedDate(dateString) {
    var n = new Date(dateString);
    var d = n.toDateString().split(" ");
    return d[1].toUpperCase() + " " + d[2];
}

var formattedDate = getFormattedDate("2011-12-31 01:00:00"); // returns "DEC 31"

Upvotes: 3

Jeffrey Sweeney
Jeffrey Sweeney

Reputation: 6114

This should work:

var date = "2011-12-31 01:00:00";

var day = date.substring(8, 10);

var month = parseInt(date.substring(5, 7));
switch(month) {
    case 1:     month="JAN";break;
    case 2:     month="FEB";break;
    case 3:     month="MAR";break;
    case 4:     month="APR";break;
    case 5:     month="MAY";break;
    case 6:     month="JUN";break;
    case 7:     month="JUL";break;
    case 8:     month="AUG";break;
    case 9:     month="SEP";break;
    case 10:    month="OCT";break;
    case 11:    month="NOV";break;
    case 12:    month="DEC";break;
}


alert(month + " " + day);

Upvotes: -1

Related Questions