Why does the month numbering of the Date object start at zero?

Asked

Viewed 661 times

11

For example:

var data:Date = new Date();
trace(data.month); //Supondo que o mês seja Janeiro: 0

I would like some details, because I know that it is not only in Actionscript and Javascript that this happens. Why not simplify the month counter so that in a conversion it is easy to understand, getting the numbers returned exactly as the months of the year (January = 1, February = 2, March = 3)?

Because we always have to decrease 1:

var dataStr:String = "28/04/2014";
var data:Date = new Date(dataStr.substr(6,4), (dataStr.substr(3,2)-1), dataStr.substr(0,2), 0, 0, 0);
trace(data.month); //3

Why does this happen?

  • Because data.month is an array and every array starts from scratch, I believe this is it.

  • @Silvioandorinha What if the day of the month starts at 1 because it is not an array? Anything that is an array so you can assign a value to each element ex: 0=>"January" or 0=>"Monday" starts at 0?

  • 1

    @Jorgeb. commenting

2 answers

5


This is because all/Enum/counters/arrays by convention start at 0

So, as it turns out... understand that they continue to be 12 months, only q they will go from 0 to 11, January to December.

If you want them to represent exactly the month q wants, just add +1.

If you create a list on javascript.

var arrayLista = [];
arrayLista.push("First");

If you have a Debugger or something like that, you can see that the structure of this Array starts as follows:

arrayLista = { [0] => "First" }

Increment again with the push, you’ll see something like this:

arrayLista = { [0] => "First", [1] = "Second" }
  • So why is the number of days 1 to 31 and not 0 to 30?

  • I understand, I believe it works the same way in relation to the day of the week (Sunday, Monday...). But why are the days not the same? And the year?

  • 3

    @biio Probably because the days and years do not need to be mapped to names, while the months and days of the week need. Anyway, it’s an arbitrary language decision.

  • 1

    The reason is that these parameters like Weekdays and Months are derived from an Array object, correct? So that’s what simplifies the conversion from string (January) to int (0). That’s why it’s used as an Array?

  • In that case you say in meses[0] and get out Janeiro?? If so, that’s why, where 0 is the position of the array that stores the string Janeiro

3

The reason why the months of the year start from scratch is a major flaw in the Java Date API design and which, in my opinion, occurs in other languages as well, such as Javascript.

This is probably the result of using an internal vector or the internal use in the C++ API JVM, as mentioned in this matter of the OS, which led Sun to choose zero as the base index of months.

I say that it was a design failure because it’s no wonder that every week, whether at work or on forums, I come across a problem caused by a misunderstanding of the developer. I myself, already knowing this, I ended up falling into the same trap a few times.

It is totally counterintuitive. For example, you receive in an input the text 2014-04-28 and makes a parse to date using the standard yyyy-MM-dd. Then you print the month and see the value 3. In my first experience with this I was sure it was a bug in my program. The same occurred with several colleagues.

However, the difference is that in other languages like Javascript the formatting of parse dates are not as frequent or necessary as in languages of back end.

  • Yes, but this occurs in other languages as well. In all of them there was this same design flaw?

  • @biio Yes, I know. Languages that have received the same influence. If it is a failure, it depends on the API in general. The point is: you see systems with bugs or displaying incorrect dates in these languages as often as in Java?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.