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

前端 未结 9 1110
长情又很酷
长情又很酷 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:07

    Here you are,

    NSDate *currentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate]; // Get necessary date components
    
     [components month]; //gives you month
     [components day]; //gives you day
     [components year]; // gives you year
    

    You can use NSDateComponents for that as above.

    Please visit this page for details.

    Hope it helps.

提交回复
热议问题