views:

115

answers:

6

How do I create a NSDate of August 1, 2005? Found NSDateComponent on the internet, but seems too complicated.

+3  A: 

What about:

[NSDate dateWithNaturalLanguageString:@"August 1st, 2005"];
zneak
LOL very funny.
Fasid
@Fasid: I'm serious. The above call returns the date you've asked for.
zneak
Interesting. Auto complete didn't pick it up. I hope it's not a private API is it?
Fasid
This is by far the easiest way to get a hard coded date and is totally legitimate.
theMikeSwan
warning: 'NSDate' may not respond to '+dateWithNaturalLanguageString:' but it doesn't seem to work
Fasid
@Fasid: No, you're right. This method only exists on Mac OS, not on iOS. I'll remove this answer.
zneak
I love it when Apple has two different implementations of the same class, NSDate on Mac OS X responds to `dateWithNaturalLanguageString:`, on iOS though it does not (guess that means users can't type in dates on iOS). This means you will have to track through NSDateComponents, it's not really that bad though the class documentation has an example.
theMikeSwan
It's kinda-deprecated on Mac OS X too, as it doesn't really handle dates very well in languages other than English.
Jonathan Grynspan
It works on iOS too, just not officially supported
Fasid
A: 

It rather depends on your timezone, but in GMT, the 1st of August 2005 at 1 minute past midnight was the UNIX timestamp 1122854401. So you can write...

[NSDate dateWithTimeIntervalSince1970:1122854401];
Adam Wright
how did you calculate 1122854401
Fasid
+5  A: 

Another possibility is to use NSDateComponents and NSCalendar. For instance:

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2000];
[comps setMonth:1];
[comps setDay:1];
[comps setHour:0];
[comps setMinute:0];
[comps setSecond:0];

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *startDate = [cal dateFromComponents:comps];

[cal release];  
[comps release];    
Jan Zich
There's no point in setting components to zero AFAIK.
Chuck
Well, if you do so, it shows intent: that you want the time to be explicitly 00:00:00, rather than "the default." Semantics are slightly different.
Jonathan Grynspan
I wouldn't bother, though, if the defaults are documented to be 0. (I haven't checked)
jtbandes
A: 

Introduction to Date and Time Programming Guide for Cocoa

NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:1];
[components setMonth:8];
[components setYear:2005];
NSCalendar *gregorian = [[NSCalendar alloc]
                         initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:components];
vikingosegundo
+1  A: 
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"M/d/yy"];
NSDate *myDate = [myFormatter dateFromString:@"August/01/05"];
Jürgen Hollfelder
A: 

Don’t miss the fact that this date will be the 2nd of August or the 31st of July on other places (and maybe something “strange”, when not using the gregorian calendar). Maybe that’s not importent for you and now. But you should be aware of this. You think, it might not hit you? I’d not be too sure. Have a look at http://stackoverflow.com/questions/3250968/nscalendar-getting-day-difference-wrong for example.

Greetings

Objective Interested Person