Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date conversion from dd/mm/yyyy to yyyy/mm/dd using javascript [duplicate]

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.

like image 306
Hormis Lona Avatar asked Jan 21 '15 06:01

Hormis Lona


2 Answers

Here's the snippet I found working

date="21/01/2015";
var newdate = date.split("/").reverse().join("-");
console.log(newdate)
like image 167
Bhargav Modi Avatar answered Sep 21 '22 17:09

Bhargav Modi


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;

Working Demo

like image 31
Manoj Avatar answered Sep 18 '22 17:09

Manoj