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

前端 未结 9 1100
长情又很酷
长情又很酷 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-07 15:54

    NSDate *currentDate = [NSDate date];
    NSCalendar* calendar = [NSCalendar currentCalendar];
    NSDateComponents* components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit fromDate:currentDate]; // Get necessary date components
    
    [components month]; //gives you month
    [components day]; //gives you day
    [components year]; // gives you year
    
    NSString *temp=[NSString stringWithFormat:@"Months:%d Day:%d Year:%d",[components month],  [components day],[components year]];
    NSLog(@"%@",temp);
    

提交回复
热议问题