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. How can I do this in iOS? I found the similar question but the method descriptionWithCalendarFormat
: gives a warning and seems to be deprecated by now.
我想用整数形式得到NSDate的日期,月份和年份,也就是说,如果日期是1988年1/2,那么我应该将1、2和1988分别作为整数。在iOS里我怎么做呢?我发现了类似的问题,但方法描述采用了calendarformat:给出了一个警告,现在似乎已经弃用了。
9 个解决方案
#1
217
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.
您可以使用NSDateComponents进行上述操作。
Please visit this page for details.
详情请浏览此网页。
Hope it helps.
希望它可以帮助。
#2
17
Yes by the use of NSCalendar, though, i think this will make your work.
是的,通过使用NSCalendar,我认为这将使你的工作。
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
#3
13
Swift
斯威夫特
let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: self)
let day = components.day
let month = components.month
let year = components.year
For convenience you can put this in an NSDate
extension and make it return a tuple:
为了方便起见,您可以将其放入NSDate扩展中,并使其返回一个tuple:
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)
}
}
Now you have to write only:
现在你只需要写下:
let (day, month, year) = date.dayMonthYear
If you wanted to e.g. get only the the year you can write:
如果你想要,例如,只得到你能写的年份:
let (_, _, year) = date.dayMonthYear
#4
9
You can use NSDateComponents to get this,
你可以用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];
#5
7
To those who just copy and paste directly from the web, like me, this is a small update for iOS 8
对于那些像我一样直接从网络上复制粘贴的人来说,这是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
The difference is NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit
have been deprecated
差异是NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay nsmonth, | nscalendar, | nsyeararunit | nsweekdaycalendar,已经被弃用
#6
4
Put it in an extension and live your life ????????
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 (It's an iteration of previous answers, but solves it for all your future app projects)
Swift 3.0(这是对以前答案的重复,但为所有未来的应用项目提供了解决方案)
#7
2
U can try this .......
你可以试试这个程序。
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];
#8
1
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);
#9
1
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)")
#1
217
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.
您可以使用NSDateComponents进行上述操作。
Please visit this page for details.
详情请浏览此网页。
Hope it helps.
希望它可以帮助。
#2
17
Yes by the use of NSCalendar, though, i think this will make your work.
是的,通过使用NSCalendar,我认为这将使你的工作。
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
#3
13
Swift
斯威夫特
let components = NSCalendar.currentCalendar().components([.Day, .Month, .Year], fromDate: self)
let day = components.day
let month = components.month
let year = components.year
For convenience you can put this in an NSDate
extension and make it return a tuple:
为了方便起见,您可以将其放入NSDate扩展中,并使其返回一个tuple:
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)
}
}
Now you have to write only:
现在你只需要写下:
let (day, month, year) = date.dayMonthYear
If you wanted to e.g. get only the the year you can write:
如果你想要,例如,只得到你能写的年份:
let (_, _, year) = date.dayMonthYear
#4
9
You can use NSDateComponents to get this,
你可以用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];
#5
7
To those who just copy and paste directly from the web, like me, this is a small update for iOS 8
对于那些像我一样直接从网络上复制粘贴的人来说,这是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
The difference is NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay
NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit
have been deprecated
差异是NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay nsmonth, | nscalendar, | nsyeararunit | nsweekdaycalendar,已经被弃用
#6
4
Put it in an extension and live your life ????????
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 (It's an iteration of previous answers, but solves it for all your future app projects)
Swift 3.0(这是对以前答案的重复,但为所有未来的应用项目提供了解决方案)
#7
2
U can try this .......
你可以试试这个程序。
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];
#8
1
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);
#9
1
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)")