Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: how to format a given datetime?

Tags:

javascript

given my data is:

2011-12-31 01:00:00

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

like image 762
Francesco Avatar asked Jan 28 '26 07:01

Francesco


1 Answers

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"
like image 74
Wayne Avatar answered Jan 30 '26 19:01

Wayne