6

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 !

BMN
  • 8,253
  • 14
  • 48
  • 80
  • Check out this thread: http://stackoverflow.com/questions/8681534/why-does-javascript-evaluate-a-2-digit-year-of-00-as-1900-instead-of-2000 – MasNotsram May 14 '13 at 08:44
  • 1
    Seems like firefox is doing something weird, as the date is stored internally in milliseconds since epoch (1970). So, it should never return 1913. [(ECMAScript Reference)](http://objjob.phrogz.net/js/object/224) – Anirudh Ramanathan May 14 '13 at 08:45
  • @SpAm thanks for the link, but it doesn't really explains why Chrome & Firefox have different behaviours on this. – BMN May 14 '13 at 08:47
  • 2
    Date parsing is JavaScript implementation dependent and I assume that Firefox just doesn't understand the `mm/dd/yy` format - see http://stackoverflow.com/questions/2587345/javascript-date-parse – andyb May 14 '13 at 08:47
  • if i give 2013 it is returning 2013 – PSR May 14 '13 at 08:47
  • until 99 it is adding 1900 .Greater than 100 it is giving the same value.Very interesting – PSR May 14 '13 at 08:49
  • see here http://www.jr.pl/www.quirksmode.org/js/datecompat.html it may helpful to you – PSR May 14 '13 at 08:51
  • 1
    Use http://momentjs.com/ maybe. The cross-browser issues with dates are really painful to deal with. – Anirudh Ramanathan May 14 '13 at 08:51
  • @andyb thanks for the link, this would explain the why. – BMN May 14 '13 at 08:53
  • @DarkCthulhu thanks too, I'll have a look at this lib ! – BMN May 14 '13 at 08:54
  • 1
    @DarkCthulhu I tried to implement the moment.js library, and on Firefox, I have the same behaviour, 13 giving 1913. – BMN May 14 '13 at 09:20

1 Answers1

5

From the spec:

However, the expression Date.parse(x.toLocaleString()) is not required to produce the same Number value as the preceding three expressions and, in general, the value produced by Date.parse is implementation-dependent when given any String value that does not conform to the Date Time String Format (15.9.1.15) and that could not be produced in that implementation by the toString or toUTCString method.

When creating a date object by passing a date string to the constructor, the method of parsing is exactly the same as Date.parse.

Your date format, using a 2 digit year, is not conforming to the standard. As such, it would seem that the way these dates are parsed is implementation specific, meaning that it depends upon the Javascript engine being used. In Google's case, it's usually V8, and in Firefox, it's TraceMonkey or Rhino I believe.

In short, you should really use 4 digit years, as there is no YY in the JS standard.

MasNotsram
  • 2,105
  • 18
  • 28
  • 1
    Thank you for this answer, this summarizes well what have been said in the comments ! – BMN May 14 '13 at 09:57