Format JavaScript Date to yyyy-mm-dd

Format JavaScript Date to yyyy-mm-dd

Hi all i have a Date format Sun May 11,2014 how can i convert it to 2014-05-11 in javascript.
function taskDate(dateMilli) {
var d = (new Date(dateMilli) + ”).split(‘ ‘);
d[2] = d[2] + ‘,’;

return [d[0], d[1], d[2], d[3]].join(‘ ‘);
}
var datemilli = Date.parse(‘Sun May 11,2014’);
taskdate(datemilli);

the above code gives me same date format sun may 11,2014 please help

Solutions/Answers:

Solution 1:

you can do

function formatDate(date) {
    var d = new Date(date),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [year, month, day].join('-');
}

usage example:

alert(formatDate('Sun May 11,2014'));

Output:

2014-05-11

Demo on fiddle: http://jsfiddle.net/abdulrauf6182012/2Frm3/

Solution 2:

Just leverage the built in toISOString method that brings your date to ISO 8601 format.

yourDate.toISOString().split('T')[0]

where yourDate is your date object.

Solution 3:

I use this way to get the date in format yyyy-mm-dd 🙂

var todayDate = new Date().toISOString().slice(0,10);

Solution 4:

The simplest way to convert your date to yyyy-mm-dd format, is to do this :

var date = new Date("Sun May 11,2014");
var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000 ))
                    .toISOString()
                    .split("T")[0];

How it works :

  • new Date("Sun May 11,2014") converts the string "Sun May 11,2014" to a date object that represents the time Sun May 11 2014 00:00:00 in a timezone based on current locale (host system settings)
  • new Date(date.getTime() - (date.getTimezoneOffset() * 60000 )) converts your date to a date object that corresponds with the time Sun May 11 2014 00:00:00 in UTC (standard time) by subtracting the time zone offset
  • .toISOString() converts the date object to ISO 8601 string 2014-05-11T00:00:00.000Z
  • .split("T") splits the string to array ["2014-05-11", "00:00:00.000Z"]
  • [0] takes the first element of that array
Related:  How to detect if javascript files are loaded?

Demo

var date = new Date("Sun May 11,2014");
var dateString = new Date(date.getTime() - (date.getTimezoneOffset() * 60000 ))
                    .toISOString()
                    .split("T")[0];

console.log(dateString);

Solution 5:

format = function date2str(x, y) {
    var z = {
        M: x.getMonth() + 1,
        d: x.getDate(),
        h: x.getHours(),
        m: x.getMinutes(),
        s: x.getSeconds()
    };
    y = y.replace(/(M+|d+|h+|m+|s+)/g, function(v) {
        return ((v.length > 1 ? "0" : "") + eval('z.' + v.slice(-1))).slice(-2)
    });

    return y.replace(/(y+)/g, function(v) {
        return x.getFullYear().toString().slice(-v.length)
    });
}

result:

format(new Date('Sun May 11,2014'), 'yyyy-MM-dd')
"2014-05-11

Solution 6:

A combination of some of the answers:

var d = new Date(date);
date = [
  d.getFullYear(),
  ('0' + (d.getMonth() + 1)).slice(-2),
  ('0' + d.getDate()).slice(-2)
].join('-');