iphone Get current year as string

后端 未结 5 1183
迷失自我
迷失自我 2021-02-01 01:33

How do I get current year as string in Obj-C ?

Also how do I compare the same using another year value ?

Is it advisable to do a string comparision OR dirctly ye

5条回答
  •  一向
    一向 (楼主)
    2021-02-01 02:30

    NSCalendar *gregorian = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    NSInteger year = [gregorian component:NSCalendarUnitYear fromDate:NSDate.date];
    

    Note: there are several calendar identifiers besides NSGregorianCalendar. Use whatever is appropriate for your locale. You can ask for whatever set of components you'd like by bitwise OR'ing the fields together (e.g., NSYearCalendarUnit | NSMonthCalendarUnit) and using components:fromDate instead. You can read about it in the Date and Time Programming Guide.

    With calendar components as primitive types, comparisons are efficient.

提交回复
热议问题