IPhone OS: How do I create an NSDate for a specific date?

It seems to be simple, but I cannot find a way to do this.

It would be great to see a couple of different methods.

+81
ios cocoa-touch nsdate nscalendar nsdateformatter
Apr 05 '10 at 17:10
source share
7 answers

@ Seagull answer is correct, and lead me to the next code. I think I would share:

NSDateComponents *comps = [[NSDateComponents alloc] init]; [comps setDay:10]; [comps setMonth:10]; [comps setYear:2010]; NSDate *date = [[NSCalendar currentCalendar] dateFromComponents:comps]; 
+187
Feb 22 2018-11-22T00:
source share

You can use this

 NSString *dateString = @"03-Sep-11"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; dateFormatter.dateFormat = @"dd-MMM-yy"; NSDate *date = [dateFormatter dateFromString:dateString]; 

Hope this helps you.

+18
Oct 29 '12 at 9:30
source share

If you're talking about a specific calendar date, not a UNIXy date, you probably need an NSCalendar dateFromComponents:

+16
Apr 05 '10 at 17:23
source share

See the documentation for NSDate . You can use the dateFromString: method of the NSDateFormatter or the dateFromComponents: method of the NSCalendar .

+8
Apr 05 '10 at 17:23
source share

Swift 3 Examples

Examples are often the easiest way to learn. Here are some examples.

Now

This one is the easiest.

 let currentDateTime = Date() 

February 20, 2017

 // Specify date components var dateComponents = DateComponents() dateComponents.year = 2017 dateComponents.month = 2 dateComponents.day = 20 // Create date from components let userCalendar = Calendar.current // user calendar let dateThisPostWasUpdated = userCalendar.date(from: dateComponents) 

April 1, 1976

 // Specify date components var dateComponents = DateComponents() dateComponents.year = 1976 dateComponents.month = 4 dateComponents.day = 1 // Create date from components let userCalendar = Calendar.current // user calendar let appleFoundedDate = userCalendar.date(from: dateComponents) 

Need more information ...?

There are other ways to create dates. If you want to know them, as well as how to display the date, then see My more complete answer .

+4
Jun 08 '16 at 8:33
source share

I found this thread looking for the answer to this question, but then I found a good example in one of my Big Nerd Ranch Objective-C programming books. Credit to them, not to me.

  NSDateComponents *myBDay =[[NSDateComponents alloc] init]; [myBDay setDay:22]; [myBDay setMonth: 04]; [myBDay setYear: 1984]; [myBDay setHour:9]; [myBDay setMinute:25]; [myBDay setSecond:35]; NSCalendar *g = [[ NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian]; NSDate *dateOfBirth = [g dateFromComponents:myBDay]; 
+3
Jul 30 '15 at 4:43
source share

This is ridiculous, isn't it?

Mid-2013, and Apple still hasn't provided an easy way to set the NSDate.

During my current iPad project, I could not believe that I had to stop the performance for a while in order to write my own helper class in order to get the Year value from NSDate. I mean, come on, this is the main material.

Anyway, here is a helper class that I used in my project to convert a string to an NSDate value:

 @implementation DateHelper +(NSDate*)parseDateString:(NSString *)dateString { NSDateFormatter *rfc3339TimestampFormatterWithTimeZone = [[NSDateFormatter alloc] init]; [rfc3339TimestampFormatterWithTimeZone setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]]; [rfc3339TimestampFormatterWithTimeZone setDateFormat:@"MMM dd, yyyy"]; NSDate *theDate = nil; NSError *error = nil; if (![rfc3339TimestampFormatterWithTimeZone getObjectValue:&theDate forString:dateString range:nil error:&error]) { NSLog(@"Date '%@' could not be parsed: %@", dateString, error); } return theDate; } @end 

Using this code, you can set the NSDate value using something like:

 NSDate* date = [DateHelper parseDateString:@"Jul 16, 2013"]; 

Note. This function was based on the code taken here: stack overflow

My solution was to use the following code, but I found that sometimes it just didn't understand and returned zero.

 // Take a date string in the format "Oct 23, 2013", and convert it into a NSDate value // THIS DOESN'T WORK ! DON'T TRUST THIS CODE !! NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"MMM dd, yyyy"]; [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]]; NSDate* date = [formatter dateFromString:dateString]; 

I remember that it failed miserably on October 12, 2012 ... that's why I gave up and used the more complex parseDateString function shown above.

My point is ... be careful.

Some of the very simple NSDate functions just don't work properly ...

+2
Jul 18 '13 at 14:17
source share



All Articles