iDev – Getting a good looking date with prefix
Author: The Man
Not sure it should take this much code but….
View Code OBJC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"MMMM yyyy"]; NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init]; [timeFormat setDateFormat:@"HH:mm"]; NSDateFormatter *dayFormat = [[NSDateFormatter alloc] init]; [dayFormat setDateFormat:@"d"]; NSDate *now = [[NSDate alloc] init]; NSString *theDate = [dateFormat stringFromDate:now]; NSString *theTime = [timeFormat stringFromDate:now]; NSString *theDay = [dayFormat stringFromDate:now]; NSString *dayWithPrefix; if ((theDay == @"1") || (theDay == @"21") || (theDay == @"31")) { dayWithPrefix = [NSString stringWithFormat:@"%@st", theDay]; } else if ((theDay == @"2") || (theDay == @"22")) { dayWithPrefix = [NSString stringWithFormat:@"%@nd", theDay]; } else if ((theDay == @"3") || (theDay == @"23")) { dayWithPrefix = [NSString stringWithFormat:@"%@rd", theDay]; } else { dayWithPrefix = [NSString stringWithFormat:@"%@th", theDay]; } [lblDateTime setText:[NSString stringWithFormat:@"Date: %@ %@ Time: %@", dayWithPrefix, theDate, theTime]]; [dateFormat release]; [timeFormat release]; [theDay release]; [now release]; |






February 1st, 2012 at 11:23 am
Thanks for the post.
Quick note
-must be a typo for
[theDay release]; should be [dayFormat release] instead. theDay is just autoreleased.
February 1st, 2012 at 11:35 am
Yes left it in there to see if anybody noticed…. or I am just not that a good programmer :)
Thanks.