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
2010-07-25 20:39:42
LOL very funny.
Fasid
2010-07-25 20:55:54
@Fasid: I'm serious. The above call returns the date you've asked for.
zneak
2010-07-25 20:59:02
Interesting. Auto complete didn't pick it up. I hope it's not a private API is it?
Fasid
2010-07-25 21:03:23
This is by far the easiest way to get a hard coded date and is totally legitimate.
theMikeSwan
2010-07-25 21:04:31
warning: 'NSDate' may not respond to '+dateWithNaturalLanguageString:' but it doesn't seem to work
Fasid
2010-07-25 21:05:46
@Fasid: No, you're right. This method only exists on Mac OS, not on iOS. I'll remove this answer.
zneak
2010-07-25 21:14:03
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
2010-07-25 21:15:15
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
2010-07-25 21:47:46
It works on iOS too, just not officially supported
Fasid
2010-07-25 22:24:59
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
2010-07-25 20:42:59
+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
2010-07-25 20:51:16
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
2010-07-25 21:47:11
I wouldn't bother, though, if the defaults are documented to be 0. (I haven't checked)
jtbandes
2010-07-26 00:02:36
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
2010-07-25 20:53:26
+1
A:
NSDateFormatter *myFormatter = [[NSDateFormatter alloc] init];
[myFormatter setDateFormat:@"M/d/yy"];
NSDate *myDate = [myFormatter dateFromString:@"August/01/05"];
Jürgen Hollfelder
2010-07-25 20:59:43
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
2010-07-25 21:53:35