如何得到整数格式的NSDate日,月和年?

我想以整数forms获得NSDate的日,月,年组件,即如果date是1/2/1988年,那么我应该把1,2和1988作为整数分开。 我如何在iOS中做到这一点? 我发现了类似的问题,但方法descriptionWithCalendarFormat :给出了一个警告,似乎现在已经废弃。

9 Solutions collect form web for “如何得到整数格式的NSDate日,月和年?”

这个给你,

 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 

你可以像上面那样使用NSDateComponents。

有关详细信息,请访问此页面 。

希望能帮助到你。

是的,通过使用NSCalendar,虽然,我认为这会使你的工作。

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

迅速

 let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: self) let day = components.day let month = components.month let year = components.year 

为了方便起见,你可以把它放在一个NSDate扩展中,并返回一个元组:

 extension NSDate: Comparable { var dayMonthYear: (Int, Int, Int) { let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: self) return (components.day, components.month, components.year) } } 

现在你只需要写:

 let (day, month, year) = date.dayMonthYear 

如果你想只得到你能写的那一年:

 let (_, _, year) = date.dayMonthYear 

你可以使用NSDateComponents来获得这个,

 NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar]; NSDate *currDate = [NSDate date]; NSDateComponents *dComp = [calendar components:( NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:currDate]; int day = [dComp day]; int month = [dComp month]; int year = [dComp year]; 

对于像我这样直接从网页复制和粘贴的用户,这是iOS 8的一个小更新

 NSCalendar* calendar = [NSCalendar currentCalendar]; NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:[NSDate date]]; [components day]; //Day [components month];//Month [components year];//Year 

区别在于NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit已被弃用

把它放在一个扩展名,并过你的生活🍸🏖

 extension Date{ var day:Int {return Calendar.current.component(.day, from:self)} var month:Int {return Calendar.current.component(.month, from:self)} var year:Int {return Calendar.current.component(.year, from:self)} } Date().day//22 Date().month//4 Date().year//2017 

Swift 3.0(这是以前答案的一个迭代,但解决了所有您未来的应用程序项目)

你可以试试这个…….

 Mycal = [NSCalendar currentCalendar]; today = [NSDate date]; compA =[Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; FirstDateofMonth=[today dateByAddingTimeInterval:([compA day]-1)*(-24*60*60)]; compA =[Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:FirstDateofMonth]; today = FirstDateofMonth; compA = [Mycal components:(NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit) fromDate:today]; [compA setMonth:([compA month])]; [self DrawMonthInPortraitView]; [compA retain]; 
 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); 
 let date = Date() let calendar = Calendar.current let year = calendar.component(.year, from: date) let month = calendar.component(.month, from: date) let day = calendar.component(.day, from: date) print("hours = \(year):\(month):\(day)") 
  • 将未知格式(任何格式)的string转换为date
  • NSDateFormatter中的“YYYY”和“yyyy”之间的区别
  • NSDateFormatter setDateFormat的序号月份后缀选项
  • NSString到NSDate
  • 有没有一种简单的方法将ISO8601时间戳转换为格式化的NSDate?
  • 处理NSDateFormatter语言环境“feechur”的最佳方法是什么?
  • NSDateFormatter与24小时的时间
  • 为什么NSDateFormatter返回这4个时区的无date?
  • 什么样的讽刺错误是这个iOS?
  • 如何将NSDate转换为“今天”,“昨天”,“一周前”,“一个月前”,“一年前”的相对格式?
  • 如何将string转换为Swift iOS中的string?