Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert DD-MM-YYYY to YYYY-MM-DD format using Javascript

Tags:

javascript

I'm trying to convert date format (DD-MM-YYYY) to (YYYY-MM-DD).i use this javascript code.it's doesn't work.

 function calbill()
    {
    var edate=document.getElementById("edate").value; //03-11-2014

    var myDate = new Date(edate);
    console.log(myDate);
    var d = myDate.getDate();
    var m =  myDate.getMonth();
    m += 1;  
    var y = myDate.getFullYear();

        var newdate=(y+ "-" + m + "-" + d);

alert (""+newdate); //It's display "NaN-NaN-NaN"
    }
like image 410
KT1 Avatar asked Nov 23 '14 08:11

KT1


People also ask

How to change Date format in JavaScript from MM DD YYYY to YYYY-MM-DD?

Re: convert Date from YYYY-MM-DD to MM/DD/YYYY in jQuery/JavaScript. var tempDate = new Date("2021-09-21"); var formattedDate = [tempDate. getMonth() + 1, tempDate.

How to change Date format DD MM YYYY in JavaScript?

To format a date as dd/mm/yyyy:Use the getDate() , getMonth() and getFullYear() methods to get the day, month and year of the date. Add a leading zero to the day and month digits if the value is less than 10 . Add the results to an array and join them with a forward slash separator.

How to display Date in DD MM YYYY format in html?

To set and get the input type date in dd-mm-yyyy format we will use <input> type attribute. The <input> type attribute is used to define a date picker or control field. In this attribute, you can set the range from which day-month-year to which day-month-year date can be selected from.

How do I change Ddmmyyyy to Mmddyyyy?

First, pick the cells that contain dates, then right-click and select Format Cells. Select Custom in the Number Tab, then type 'dd-mmm-yyyy' in the Type text box, then click okay. It will format the dates you specify.


1 Answers

This should do the magic

var date = "03-11-2014";
var newdate = date.split("-").reverse().join("-");
like image 94
Ehsan Avatar answered Oct 12 '22 18:10

Ehsan