Because this is apparently my most popular answer, I'll try to edit it to contain a little bit more information.
Despite its name, NSDate
in and of itself simply marks a point in machine time, not a date. There's no correlation between the point in time specified by an NSDate
and a year, month, or day. For that, you have to refer to a calendar. Any given point in time will return different date information based on what calendar you're looking at (dates are not the same in both the Gregorian and Jewish calendars, for instance), and while the Gregorian calendar is the most widely used calendar in the world - I'm assuming - we're a little biased that NSDate
should always use it. NSDate
, luckily, is far more bipartisan.
Getting date and time is going to have to pass through NSCalendar
, as you mentioned, but there's a simpler way to do it:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
That generates an NSDateComponents
object containing the day, month, and year from the current system calendar for the current day. (Note: this isn't necessarily the current user-specified calendar, just the default system one.)
Of course, if you're using a different calendar or date, you can easily change that. A list of available calendars and calendar units can be found in the NSCalendar
Class Reference. More information about NSDateComponents
can be found in the NSDateComponents
Class Reference.
For reference, accessing individual components from the NSDateComponents
is rather easy:
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
You just have to be mindful: NSDateComponents
won't contain valid information for any fields you ask for unless you generated them with that valid information (i.e. request NSCalendar
to provide that information with NSCalendarUnit
s). NSDateComponents
contain no reference information in and of themselves - they're just simple structures that hold numbers for you to access. If you want to also get an era, for instance, out of NSDateComponents
, you'll have to feed the generator method from NSCalendar
with the NSCalendarUnitEra
flag.
You can get separate component of a NSDate using NSDateFormatter:
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"dd"];
myDayString = [df stringFromDate:[NSDate date]];
[df setDateFormat:@"MMM"];
myMonthString = [df stringFromDate:[NSDate date]];
[df setDateFormat:@"yy"];
myYearString = [df stringFromDate:[NSDate date]];
If you wish to get month's number instead of abbreviation, use "MM". If you wish to get integers, use [myDayString intValue];
Just to reword Itai's excellent (and working!) code, here's what a sample helper class would look like, to return the year value of a given NSDate variable.
As you can see, it's easy enough to modify this code to get the month or day.
+(int)getYear:(NSDate*)date
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:date];
int year = [components year];
int month = [components month];
int day = [components day];
return year;
}
(I can't believe we're having to write our own basic iOS date functions like this, in 2013...)
One other thing: don't ever use < and > to compare two NSDate values.
XCode will happily accept such code (without any errors or warnings), but its results are a lottery. You must use the "compare" function to compare NSDates:
if ([date1 compare:date2] == NSOrderedDescending) {
// date1 is greater than date2
}
In Swift 2.0:
let date = NSDate()
let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)!
let components = calendar.components([.Month, .Day], fromDate: date)
let (month, day) = (components.month, components.day)
I'm writing this answer because it's the only approach that doesn't give you optionals back from NSDateComponent
variables and/or force unwrapping those variables (also for Swift 3).
Swift 3
let date = Date()
let cal = Calendar.current
let year = cal.component(.year, from: date)
let month = cal.component(.month, from: date)
let day = cal.component(.day, from: date)
Swift 2
let date = NSDate()
let cal = NSCalendar.currentCalendar()
let year = cal.component(.Year, fromDate: date)
let month = cal.component(.Month, fromDate: date)
let day = cal.component(.Day, fromDate: date)
Bonus Swift 3 fun version
let date = Date()
let component = { (unit) in return Calendar.current().component(unit, from: date) }
let year = component(.year)
let month = component(.month)
let day = component(.day)
ObjC
NSDate *date = [NSDate date];
NSInteger era, year, month, day;
[[NSCalendar currentCalendar] getEra:&era year:&year month:&month day:&day fromDate:date];
Swift
let date = NSDate.init()
var era = 0, year = 0, month = 0, day = 0
NSCalendar.currentCalendar().getEra(&era, year:&year, month:&month, day:&day, fromDate: date)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With