I need to convert a date from dd/mm/yyyy to yyyy/mm/dd. I am getting the input from a text box in dd/mm/yyyy format.When i try to convert it to yyyy/mm/dd i got the error invalid date.How can i convert it properly.
            Asked
            
        
        
            Active
            
        
            Viewed 5.4k times
        
    15
            
            
        - 
                    2Can you show the code you're using now? – Wesley Smith Jan 21 '15 at 06:42
- 
                    What did you try yourself ? – Pratik Joshi Jan 21 '15 at 06:42
- 
                    var start=request.param('startDate'); formatedStartDate = getYearMonthDate(new Date(start)); function getYearMonthDate(date){ var monthYear=date.getFullYear()+" "+date.getMonth()+ " " + date.getDate(); return monthYear; } – Hormis Lona Jan 21 '15 at 06:44
- 
                    1`date="dd/mm/yyyy"; var newdate = date.split("/").reverse().join("-");` – Bhargav Modi Jan 21 '15 at 06:45
- 
                    @BhargavModi , please post it as an answer. – Pratik Joshi Jan 21 '15 at 06:45
- 
                    @jQuery.PHP.Magento.com done I didnot understood your moto behind it – Bhargav Modi Jan 21 '15 at 06:47
- 
                    1@BhargavModi , Hello , If you put it as answer , it will be helpful for future visitors to see answer.Do you think they will be reading Comments to get answer of the question? – Pratik Joshi Jan 21 '15 at 06:50
2 Answers
23
            
            
        Here's the snippet I found working
date="21/01/2015";
var newdate = date.split("/").reverse().join("-");
console.log(newdate) 
    
    
        Not a bug
        
- 4,286
- 2
- 40
- 80
 
    
    
        Bhargav Modi
        
- 2,605
- 3
- 29
- 49
18
            Try this
var date= '21/01/2015';
var d=new Date(date.split("/").reverse().join("-"));
var dd=d.getDate();
var mm=d.getMonth()+1;
var yy=d.getFullYear();
var newdate=yy+"/"+mm+"/"+dd;
 
    
    
        Manoj
        
- 4,951
- 2
- 30
- 56
