Advertisements
Date formatting
NSDateFormatter is the class used for formatting dates in iOS. Following table shows the symbols and corresponding usage.
Symbol | Description for symbol |
yyyy | Year in full. eg. 2017 for 2017 and 0100 for 1st century (with prefix zeros if necessary) |
yy | Year in two digits. eg. 17 for 2017 |
y | Year in 4 digit. eg. 17 for 2017, 100 for 1st century(Year 0100) |
MMMM | Month in full. eg. November |
MMM | Month in 3 letters. eg. Nov. |
MM | Month in numbers. eg. 09 for Nov |
M | Month in numbers. eg. 9 for Nov |
MMMMM | Months first letter eg. N for Nov. |
dd | Day in numbers with prefix zero if necessary. Eg. 01 for 1. |
d | Day in numbers with no prefix zero. Eg. 1 for 1st January. |
h | Hour in 12 hour format with no prefix zero. Eg. 1 for 1 pm. |
hh | Hour in numbers with prefix zero if necessary. Eg. 01 pm for 1 pm. |
HH | Hour in 24 hour format with prefix zero if necessary. Eg. 09 for 09:00 hours. |
H | Hour in 24 hour format with no prefix zero. Eg. 9 for 09:00 hours |
a | AM or PM for 12 hour formats. |
m | Minutes in numbers with no prefix zero. Eg. 1 for 1 minute. |
mm | Minutes in numbers with prefix zeros if necessary. Eg. 01 for 1 minute. |
s | Seconds in numbers with no prefix zero. Eg. 1 for 1 second. |
ss | Seconds in numbers with prefix zeros if necessary. Eg. 01 for 1 second |
zzz | Timezone in short example GMT+5:30 |
zzzz | Timezone in full, example Central Time Zone for CST. |
Z | RFC time zone format - eg. -0600, +0530 |
ZZZZZ | ISO 8601 time zone format - eg. -06:00, +05:30 |
E | Day in Short. eg W |
EEE | Day in 3 letters. eg Wed |
EEEE | Day in full. eg Wednesday |
How to get a convert date(NSDate) to server string - yyyy-MM-ddTHH:mm:ss
Code
+ (NSString *)serverDateStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; return [dateFormatter stringFromDate:date]; }
Result
2017-12-08T04:38:13
How to get a date(NSDate) from server string - yyyy-MM-ddTHH:mm:ss
Code
- (NSDate *)dateFromServerString:(NSString *)serverDateString { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"]; return [dateFormatter dateFromString:serverDateString]; }
How to get date with format Day, Month Date,Year (EEEE, MMMM dd, yyyy)?
Code
- (NSString *)stringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"EEEE, MMMM dd, yyyy"]; return [dateFormatter stringFromDate:date]; }
Result
Friday, December 08, 2017
How to get date with format Day, Month Date,Year (EEE, MMM dd, yyyy)?
Code
- (NSString *)stringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"EEE, MMM dd, yyyy"]; return [dateFormatter stringFromDate:date]; }
Result
Fri, Dec 08, 2017
How to get date with 12 hour format Hours:Minutes AM/PM (hh:mm a)?
Code
- (NSString *)stringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"hh:mm a"]; return [dateFormatter stringFromDate:date]; }
Result
04:53 AM
How to get date with 24 hour format Hours:Minutes(HH:mm)?
Code
- (NSString *)stringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"HH:mm a"]; return [dateFormatter stringFromDate:date]; }
Result
04:53
How to get date with 12 hour format Hours:Minutes:Seconds AM/PM (hh:mm:ss a)?
Code
- (NSString *)stringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"hh:mm:ss a"]; return [dateFormatter stringFromDate:date]; }
Result
04:53:23 AM
How to get date with 24 hour format Hours:Minutes:seconds(HH:mm:ss)?
Code
- (NSString *)stringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateFormat:@"HH:mm:ss a"]; return [dateFormatter stringFromDate:date]; }
Result
04:53:23
How to get a full style date from current date (NSDate) - Day Month Date, Year(EEEE, MMMM d,yyyy)?
Code
- (NSString *)fullStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:NSDateFormatterFullStyle]; return [dateFormatter stringFromDate:date]; }
Result
Friday, December 8, 2017
How to get a long style date from current date (NSDate) - Month Date, Year(MMMM d,yyyy)?
Code
- (NSString *)fullStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:NSDateFormatterFullStyle]; return [dateFormatter stringFromDate:date]; }
Result
December 8, 2017
How to get a medium style date from current date (NSDate) - Month Date, Year(MMM d,yyyy)?
Code
- (NSString *)mediumStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:NSDateFormatterMediumStyle]; return [dateFormatter stringFromDate:date]; }
Result
Dec 8, 2017
How to get a short style date from current date (NSDate) - Month Date, Year(MM/d,yy)?
Code
- (NSString *)shortStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setDateStyle:NSDateFormatterShortStyle]; return [dateFormatter stringFromDate:date]; }
Result
12/8/17
How to get a full style time from current date (NSDate) - Hour:Minutes AM/PM TimeZone(h:mm a zzzz)?
Code
- (NSString *)fullStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setTimeStyle:NSDateFormatterFullStyle]; return [dateFormatter stringFromDate:date]; }
Result
4:57:52 AM India Standard Time
How to get a long style time from current date (NSDate) - Hour:Minutes AM/PM TimeZone(h:mm a zzz)?
Code
- (NSString *)fullStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setTimeStyle:NSDateFormatterFullStyle]; return [dateFormatter stringFromDate:date]; }
Result
4:57:52 AM GMT+5:30
How to get a medium style time from current date (NSDate) - Hour:Minutes:Seconds AM/PM(h:mm:ss a)?
Code
- (NSString *)mediumStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setTimeStyle:NSDateFormatterMediumStyle]; return [dateFormatter stringFromDate:date]; }
Result
4:57:52 AM
How to get a short style time from current date (NSDate) - Hour:Minutes AM/PM(h:mm a)?
Code
- (NSString *)shortStyleStringFromDate:(NSDate *)date { NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; return [dateFormatter stringFromDate:date]; }
Result
4:57 AM
Note
You can use both setTimeStyle and setDateStyle to get a combination of both.