Format JavaScript date as yyyy-mm-dd

0 votes

I have a date with the format Sun May 11,2014. How can I convert it to 2014-05-11 using 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'); 
              console.log(taskDate(datemilli));

 
The code above gives me the same date format, sun may 11,2014. How can I fix this? Any help will be appreciated!!

Feb 23 in Java-Script by Soham
• 8,730 points
866 views

1 answer to this question.

0 votes

You could try using the following lines of code:

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('-'); 
} 

console.log(formatDate('Sun May 11,2014'));


Usage example:

console.log(formatDate('Sun May 11,2014'));

Output:

2014-05-11

answered Feb 23 by Aditya
• 7,280 points

Related Questions In Java-Script

0 votes
0 answers
0 votes
1 answer

How to pass an array as a function parameter in JavaScript?

Hello @kartik, Use: const args = ['p0', 'p1', 'p2']; call_me.apply(this, ...READ MORE

answered Sep 4, 2020 in Java-Script by Niroj
• 82,720 points
472 views
0 votes
1 answer

How to get current date and time in JavaScript?

Hello @kartik, To get time and date you ...READ MORE

answered Sep 18, 2020 in Java-Script by Niroj
• 82,720 points
104 views
0 votes
1 answer

What is callback function in JavaScript?

callback is not a keyword, its just ...READ MORE

answered Jan 30, 2020 in Java-Script by Niroj
• 82,720 points
9,644 views
0 votes
1 answer

How to clone a JavaScript object?

Hello, You can clone an object and remove ...READ MORE

answered Apr 2, 2020 in Java-Script by Niroj
• 82,720 points
201 views
0 votes
1 answer

How to Scroll to the top of the page using JavaScript?

Hii @kartik, Using javascript <script> $("a[href='#top']").click(function() { ...READ MORE

answered Apr 2, 2020 in Java-Script by Niroj
• 82,720 points
296 views
0 votes
1 answer

Convert a date format in PHP

Use strtotime() and date(): $originalDate = "2010-03-21"; $newDate ...READ MORE

answered Feb 17 in Others by Aditya
• 7,280 points
58 views
0 votes
1 answer

How do I get the current date in JavaScript?

To ensure that you get the current ...READ MORE

answered Feb 18 in Java by Aditya
• 7,280 points
27 views
0 votes
1 answer

Parsing a string to a date in JavaScript

The best string format for string parsing ...READ MORE

answered Feb 18 in Java by Aditya
• 7,280 points
27 views
0 votes
1 answer

Compare two dates with JavaScript

 The Date object will do what you ...READ MORE

answered Feb 18 in Java by Rahul
• 8,980 points
31 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP