r/learnobjectivec • u/Guisseppi • Oct 13 '14
the correct way to format an NSDate Object
as requested from /u/RossistBoss I'm making this post to explain you guys how to format an NSDate object to meet our requierements and/or needs.
so, first things first, we need an NSDate Object
NSDate *date = self.datePicker.date;
remember, this is just an example so lets assume some secondary stuff.
now that we have an NSDate object we are going to declare an NSDateFormatter object, this can be declared within the method you are currently using if this date format is just a one time thing, or you could as well declare it on your header file for all the methods to have acces to it, either way we need to give our formatter a format
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"MM/dd/yyyy"];
once we declare the formatter object we need to call the setDateFormat method which recieves an NSString with the format you would like for your date object, this could easily be as well:
[formatter setDateFormat:@"yyyy"]; or
[formatter setDateFormat:@"MM"]; or
[formatter setDateFormat:@"dd"];
once we have decided how we are going to format our NSDate object we need to actually pass it to the date object we want to format
NSString *myDate = [formatter stringFromDate:date];
now if we NSLog the string we just created we are going to have something like this:
10/12/2014
r/learnobjectivec • u/Rossistboss • Sep 14 '14
How to remove the "time" from an NSDate
- (NSDate *)removeTimeFromDate:(NSDate *)date {
unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;
NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];
return dateComponents.date;
}
r/learnobjectivec • u/rab_eye • Dec 12 '13
Learn Objective-C on Code School
codeschool.comr/learnobjectivec • u/Jcdesimp • Jun 28 '13
Ry's Objective-C Tutorial, A great tutorial that I'm using.
rypress.comr/learnobjectivec • u/Rossistboss • Jun 18 '13
Welcome to /r/learnobjectivec
Welcome to /r/learnobjectivec, a brand new subreddit that is very helpful to people who are learning the Objective C programming language.