Reputation: 21
I've an input text box with value ("2014-12-24"),I want to convert to ("3/24/2014"), So how to convert the date format in javaScript? Please help me.
Upvotes: 0
Views: 48
Reputation: 4792
You can use the below two ways to change the format as desired-
First:
var originalFormat = new Date('2014-12-24');
var newFormat = (originalFormat.getMonth() + 1)+'-'+originalFormat.getDate()+'-'+originalFormat.getFullYear();
alert(newFormat);
Fiddle Url: http://jsfiddle.net/omdk18Lx/2/
Second:
Use this library- https://github.com/jacwright/date.format
See its usage- http://jacwright.com/projects/javascript/date_format/
Upvotes: 2
Reputation: 1491
You could look at .split() and split the string on '-'
var oldDate = "2014-12-24";
var arrDate = oldDate.split('-');
//arrDate has three elements (y, m, d)
var newDate = arrDate[1]+'/'+arrDate[2]+'/'+arrDate[0];
//newDate = 12/24/2014
Upvotes: 0