97

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.

Hitesh Sahu
  • 41,955
  • 17
  • 205
  • 154
Yogi
  • 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 Answers9

224

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.

Alex Zavatone
  • 4,106
  • 36
  • 54
Janak Nirmal
  • 22,706
  • 18
  • 63
  • 99
17

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];
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
user755278
  • 1,634
  • 3
  • 16
  • 32
14

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
User
  • 31,811
  • 40
  • 131
  • 232
  • 2
    I 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
9

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)

Sentry.co
  • 5,355
  • 43
  • 38
9

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];
7

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

Jansen
  • 139
  • 1
  • 3
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];
Dinesh_
  • 259
  • 1
  • 5
  • 9
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);
annu
  • 137
  • 4
  • 12
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)")
oscarr
  • 529
  • 5
  • 6
  • 1
    While 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