I am working with jQuery.
I have date as August 2, 2016
format.
Now, I want to convert this date into Y-m-d format as 2016-08-02.
So, What jQuery should I have to write to resolve this problem?
I am working with jQuery.
I have date as August 2, 2016
format.
Now, I want to convert this date into Y-m-d format as 2016-08-02.
So, What jQuery should I have to write to resolve this problem?
you can use this code for Y-m-d
var date = new Date(userDate),
yr = date.getFullYear(),
month = date.getMonth(),
day = date.getDate(),
newDate = yr + '-' + month + '-' + day;
console.log(newDate);
or this for YYYY-mm-dd
var date = new Date(userDate),
yr = date.getFullYear(),
month = date.getMonth() < 10 ? '0' + date.getMonth() : date.getMonth(),
day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate(),
newDate = yr + '-' + month + '-' + day;
console.log(newDate);
Date.prototype.yyyymmdd = function() {
var yyyy = this.getFullYear().toString();
var mm = (this.getMonth()+1).toString(); // getMonth() is zero-based
var dd = this.getDate().toString();
return yyyy + '-' + (mm[1]?mm:"0"+mm[0]) + '-' + (dd[1]?dd:"0"+dd[0]);
};
d = new Date();
$('#today').html(d.yyyymmdd());