r/learnobjectivec Oct 13 '14

the correct way to format an NSDate Object

4 Upvotes

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 Sep 14 '14

How to remove the "time" from an NSDate

2 Upvotes
- (NSDate *)removeTimeFromDate:(NSDate *)date {
    unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;
    NSDateComponents *dateComponents = [[NSCalendar currentCalendar] components:unitFlags fromDate:[NSDate date]];
    return dateComponents.date;
}

r/learnobjectivec Jul 24 '14

How to use CocoaPods in 5 Steps

Thumbnail medium.com
4 Upvotes

r/learnobjectivec Dec 12 '13

Learn Objective-C on Code School

Thumbnail codeschool.com
6 Upvotes

r/learnobjectivec Jun 28 '13

Ry's Objective-C Tutorial, A great tutorial that I'm using.

Thumbnail rypress.com
4 Upvotes

r/learnobjectivec Jun 18 '13

Objective C book for beginners

Thumbnail amzn.com
3 Upvotes

r/learnobjectivec Jun 18 '13

Welcome to /r/learnobjectivec

2 Upvotes

Welcome to /r/learnobjectivec, a brand new subreddit that is very helpful to people who are learning the Objective C programming language.