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
The simplest way to convert your date to yyyy-mm-dd format, is to do this :
How it works :
new Date("Sun May 11,2014")
converts the string"Sun May 11,2014"
to a date object that represents the timeSun 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 timeSun May 11 2014 00:00:00
in UTC (standard time) by subtracting the time zone offset.toISOString()
converts the date object to ISO 8601 string2014-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 arrayDemo
toISOString()
assumes your date is local time and converts it to UTC. You will get incorrect date string.The following method should return what you need.
Source: https://blog.justin.kelly.org.au/simple-javascript-function-to-format-the-date-as-yyyy-mm-dd/
result:
Date.js is great for this.
This worked for me, and you can paste this directly into your HTML if needed for testing:
This worked for me to get the current date in desired format (YYYYMMDD HH:MM:SS