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, 2022 in Java-Script by Soham
• 9,710 points
4,680 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, 2022 by Aditya
• 7,680 points

Related Questions In Java-Script

0 votes
0 answers

How do I format a date in JavaScript?

May 7, 2022 in Java-Script by narikkadan
• 63,600 points
466 views
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,840 points
1,729 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,840 points
598 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,840 points
18,920 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,840 points
930 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,840 points
1,100 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, 2022 in Others by Aditya
• 7,680 points
3,851 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, 2022 in Java by Aditya
• 7,680 points
584 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, 2022 in Java by Aditya
• 7,680 points
639 views
0 votes
1 answer

Compare two dates with JavaScript

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

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