我想以整数形式获取NSDate
的日、月和年分量,即如果日期是 1/2/1988,那么我应该分别得到 1、2 和 1988 作为整数。如何在 iOS 中执行此操作?我发现了类似的问题,但该方法descriptionWithCalendarFormat
:发出警告,现在似乎已弃用。
你来了,
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];
Swift
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];
把它放在一个扩展中,过你的生活 🍸🏖
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(这是以前答案的迭代,但可以解决您未来所有应用程序项目的问题(
对于那些像我一样直接从网络上复制和粘贴的人来说,这是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
已被弃用
你可以试试这个......
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)")