Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Java by (13.1k points)
Can anyone help me how I can able to change the date format from Sun May 11, 2017 to 2017-05-11 in JavaScript?

1 Answer

0 votes
by (26.7k points)

You can try the below code:

function ChangeDateFormat(date) {

    var i = new Date(date),

        month = '' + (i.getMonth() + 1),

        day = '' + i.getDate(),

        year = i.getFullYear();

    if (month.length < 2) 

        month = '0' + month;

    if (day.length < 2) 

        day = '0' + day;

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

}

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

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

I hope this will help.

Want to become a Java expert? join Java Certification now!!

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
1 answer
asked Dec 4, 2020 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...