31

So in Swift days start counting from 1 and months start from 0. Because why not 🤷‍♂️

Comments
  • 7
    Sounds like javascript.
  • 1
    But that makes sense from a development standpoint, doesn't it? Want to get the name of the month from an array - the month can be used as the index in that array. Want to get the numerical representation of the month - +1 it. It seems like a better approach than to have to - 1 it for the first case just to get the benefit of not having to +1 it when needing the numerical representation of it.
  • 3
    @xprnio that might be true but it's more important to be consistent so it's much easier to code and later read.
    It doesn't seem like a big deal but when you're reading some code you don't want to have to think about these things.
  • 0
    How are you getting the month value? Date is based on a timestamp so it doesn’t have a month field, and my understanding was Calendar.components returns 1-based months.
  • 0
    The Calendar library is actually pretty handy. See component(from:) in https://developer.apple.com/documen....
  • 2
    @xprnio no, it doesn't make sense? Months can be identified by numbers, so why the fuck would i want my month value to be 4 when it's representing 5th month (May)? I don't care that there's a function that converts from numbers to names, it should be the function's job to minus 1 from value not your job.
  • 4
    @xprnio No I disagree. If the day and year values are both 1-based, the month should be too. Just because you *could* look up a month name in an array doesn't mean that you necessarily have to. If I were just writing a date in numeric form, there's no array and nothing to index. It doesn't make sense that I would have to +1 the month field but not the others. Consistency is what matters here.

    The Calendar library does it right though: it returns the date component as a value of the count of that many units. So the fifth month would have a month component value of 5.

    I just tested it and it does return 1-based months.
  • 0
    @sSam @devios1 Then let's put it this way. The date is always a number, same goes for a year. But a month isn't always a number. Yes, it can be represented in one, but do you tell your friends that "Hey, meet me on the 7th of the 3rd month" or do you say "meet me on the 7th of March"? That's at least how I reason with it. Same goes for days (0-6). You don't ask someone to meet you on the 3rd day of the week you ask them to meet you on Wednesday. How doesn't this make sense?
  • 0
    @devios1 Well I had the problem with the Calendar library. Didn't try to read a month value, but if you construct a Date object out of Date components through the current calendar object, you need to index the months with 0.
  • 2
    @xprnio so you either create date instance like this:
    Date.create(2019, 3, 17)
    or like this
    Date.create(2019, "march", 17).

    I don't see where 0 indexing comes in in your argument.
    2019 February is 2019/02 not 2019/01.
  • 0
    @sSam in that example I meant in a non-programming way, since the other example didn't seem to be understandable to you
  • 1
    @xprnio so? Like I said 2019 February is 2019/02 not 2019/01.
  • 0
    @sSam and there's a difference between creating a date from a string (which has a standard) and variables (which work the way they're implemented). Or are you telling me that if you have a date, and get the string representation of it, it will still have the month as a number from 0 to 11?
  • 0
    @sSam TL;DR: git gud scrub. Coding isn't always about what makes sense to you.
  • 3
    I think the writers of the Swift language got Schwifty when working on dates
  • 0
    @xprnio I explained to you why it should be 1 indexed in a no coding example, you didn't even catch that and had to pin point it to you and now you say some shit like strings are standardized and variables are not? do you know what a variable and string is? string is a type, so you know you can have variable that's of type string...
    Point is if:
    date.getMonthName() returns February.
    date.getMonth() should return 2.
    because (saying this for 3rd time):
    2019 February is 2019/02.
    Arrays have nothing to do with this at all. NOTHING. If some function somewhere uses an array when working with dates it doesn't make any difference at all. This is a conceptual question. February is represented as 2019/02...

    Should I expect some other guy to return January as 03 just because somewhere in the function he uses first 3 elements in array for his pet names?
  • 0
    Well, in Java, years start at -1900
Add Comment