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.

- 41,955
- 17
- 205
- 154

- 3,578
- 3
- 35
- 56
-
See [Convert NSDate to an Integer](http://stackoverflow.com/questions/27059292/convert-nsdate-to-an-integer) if only a single integer is needed for `NSDate`. – Suragch Jun 08 '16 at 10:00
9 Answers
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.

- 4,106
- 36
- 54

- 22,706
- 18
- 63
- 99
-
1+1 for providing the code for the correct answer. But you should mention that the info comes from NSDateComponents, not NSDateFormatter. – Black Frog Jun 02 '11 at 12:24
-
-
1@ Janak Nirmal Write be specific point you could not explain how to get string format for day, month,year, an User could not understand. – annu Mar 31 '15 at 12:12
-
@annu What do you mean ? You mean you can not understand the answer given ? – Janak Nirmal Apr 01 '15 at 11:39
-
1
-
3To avoid deprecation warnings: NSDateComponents* components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate: currentDate]; – Rodrigo Pinto Dec 16 '15 at 21:33
Yes by the use of NSCalendar, though, i think this will make your work.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

- 34,169
- 30
- 118
- 167

- 1,634
- 3
- 16
- 32
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:
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

- 31,811
- 40
- 131
- 232
-
2I like your approach. A suggestion though is to change the parameter naming to `(day: Int, month: Int, year: Int)`, that way you can use the result by using `date.dayMonthYear.day`, `date.dayMonthYear.month`, `date.dayMonthYear.year`. – Paul Peelen Jan 13 '16 at 08:44
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)

- 5,355
- 43
- 38
You can use NSDateComponents to get this,
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];

- 6,005
- 5
- 34
- 58
To those who just copy and paste directly from the web, like me, this is a small update for 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

- 139
- 1
- 3
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];

- 259
- 1
- 5
- 9
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);

- 137
- 4
- 12
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)")

- 529
- 5
- 6
-
1While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. – Donald Duck Mar 06 '17 at 13:17