How to get NSDate day, month and year in integer format?

前端 未结 9 1074
长情又很酷
长情又很酷 2020-12-07 15:31

I want to get the day, month and year components of NSDate in integer form i.e. if the date is 1/2/1988 then I should get 1, 2 and 1988 separately as an integer

9条回答
  •  旧巷少年郎
    2020-12-07 16:02

    Yes by the use of NSCalendar, though, i think this will make your work.

    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
    NSInteger day = [components day];    
    NSInteger month = [components month];
    NSInteger year = [components year];
    

提交回复
热议问题