How do I calculate the number of days in this year in Objective C

前端 未结 12 2138
旧巷少年郎
旧巷少年郎 2020-12-06 03:07

How can i calculate the number of days in a year for any calendar, not just gregorian. I have tried this

NSUInteger *days = [[NSCalendar currentCalendar] range

12条回答
  •  不知归路
    2020-12-06 04:00

    I don't know objective-c but it is a simple algorithm to determine if it is a leap year.

    if ( year % 400 == 0 )
       then 366 // Leap Year
    else if ( year % 100 == 0 )
       then 365 // Non-Leap Year
    else if ( year % 4 == 0 )
       then 366 // Leap Year
    else
       365 // Non-Leap Year
    

    Or if you want a little less verbose version

    if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
       then 366 // Leap Year
    else
       365 // Non-Leap Year
    

提交回复
热议问题