I'm trying to write some javascript code to format a date as I want, but I have troubles making it work on Firefox (it's working as I want on Chrome).
The input I have in a form is 05/01/13
(mm/dd/yy) and I want 2013-05-01
(yyyy/mm/dd).
For that, what I did is something like this :
var formDate = document.getElementById("start").value;
var myDate = new Date(formDate);
var startDate = new Date();
startDate.setMonth(myDate.getMonth() + 1);
startDate.setFullYear(myDate.getFullYear());
var FormattedDate = startDate.getFullYear() + "-" + ((startDate.getMonth() <= 10) ? "0" : "") + startDate.getMonth() + "-01"; // the day is always 01
alert(FormattedDate);
You can try it on both browsers here : http://jsfiddle.net/j4BLH/
On Google Chrome, this code gives me for example 2013-05-01
for May, but on Firefox, I have 1913-05-01
.
I know I could've written something like "20" + startDate.getYear()
but I was wondering why the results were different from Chrome to Firefox ? And maybe if you have a better way of writing the code I pasted here, please let me know :)
Thanks !