Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple NSDate Components - Easy to break into pieces
NSDate *currDate = [NSDate date]; //Current Date
NSDateFormatter *df = [[NSDateFormatter alloc] init];
//Day
[df setDateFormat:@"dd"];
NSString* myDayString = [NSString stringWithFormat:@"%@", [df stringFromDate:currDate]];
//Month
[df setDateFormat:@"MM"]; //MM will give you numeric "03", MMM will give you "Mar"
NSString* myMonthString = [NSString stringWithFormat:@"%@", [df stringFromDate:currDate]];
//Year
[df setDateFormat:@"yy"];
NSString* myYearString = [NSString stringWithFormat:@"%@", [df stringFromDate:currDate]];
//Hour
[df setDateFormat:@"hh"];
NSString* myHourString = [NSString stringWithFormat:@"%@", [df stringFromDate:currDate]];
//Minute
[df setDateFormat:@"mm"];
NSString* myMinuteString = [NSString stringWithFormat:@"%@",[df stringFromDate:currDate]];
//Second
[df setDateFormat:@"ss"];
NSString* mySecondString = [NSString stringWithFormat:@"%@", [df stringFromDate:currDate]];
//NSLog(@"Year: %@, Month: %@, Day: %@, Hour: %@, Minute: %@, Second: %@", myYearString, myMonthString, myDayString, myHourString, myMinuteString, mySecondString);
//[df release]; //if not ARC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.