0

I have this below code where i am trying to alert the date selected on click of an image. I am getting the alert but the issue is date format.

Below is alert i am getting. I need the alert in mm/dd/yyy.

And also why i am getting Thu Jan 01 1970 if i use todatestring() method.

Below is the javascript code

learningmode
  • 123
  • 1
  • 8
  • What's your intent in the addition of the two dates? – Tah Oct 07 '16 at 17:57
  • From input tag i am displaying the dates in a div tag container. In the code u can see image after clicking i need to display those dates in input tag. – learningmode Oct 07 '16 at 17:59

3 Answers3

0

Try this function to return mm/dd/yyyy

function getmmDdYy(thisDate) {
        return ('0' + (thisDate.getMonth() + 1)).slice(-2) +
            '/' +
            ("0" + (thisDate.getDate())).slice(-2) +
            '/' +
            thisDate.getFullYear();
    }

So on your fnUpdateDate(), do this:

function fnUpdateDate(date1,date2){
alert(getmmDdYy(new Date(date1)) +" ss "+ getmmDdYy(new Date(date2)));
}

Let me know if this works.

Ross
  • 119
  • 5
0

You can retrieve the single components from a date (day, month, year) and build a string with them (https://stackoverflow.com/a/30272803).

If you add the library moment.js you will be able to use formatting strings like YYYYMMDD (https://stackoverflow.com/a/27635223).

Community
  • 1
  • 1
mm759
  • 1,350
  • 1
  • 7
  • 7
0
You can pass your date to this function and return the formate as per your need

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 (month+"/"+day+"/"+year);  
                    } 
Ganesh Devkate
  • 99
  • 1
  • 12