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
  • 38,157
  • 14
  • 182
  • 142
Yogi
  • 3,538
  • 3
  • 33
  • 54
  • 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.

Muhammad Dyas Yaskur
  • 5,382
  • 10
  • 37
  • 61
Janak Nirmal
  • 22,605
  • 18
  • 61
  • 96
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
  • 33,955
  • 30
  • 116
  • 165
user755278
  • 1,636
  • 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,017
  • 39
  • 128
  • 219
  • 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
  • 4,826
  • 41
  • 35
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_
  • 249
  • 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
  • 509
  • 5
  • 5
  • 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