Reputation: 2872
This is my method which gets executed when UIDatePicker time is selected:
-(void) datePickerAction: (UIDatePicker *)sender {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale systemLocale]];
[dateFormatter setDateFormat:@"EEE MMM dd HH:mm:ss zzzz yyyy"];
self.dateTextView.text = [dateFormatter stringFromDate:self.datePicker.date];
}
This gives me date as Tue M09 29 10:26:29 GMT+05:30 2015
I want my month to appear in three alphabet format like OCT or JAN.
Surprisingly, if i comment line [dateFormatter setLocale:[NSLocale systemLocale]];
then i get month in correct way as Tue Sep 29 10:26:29 Indian Standard Time 2015
with same date formatter. Please tell me where I am going wrong.
My desired date format example is Tue Sep 29 10:26:29 GMT+05:30 2015
Upvotes: 3
Views: 718
Reputation: 2872
I got little idea on the root cause of my problem after reading to Amin Negm-Awad's answer. So, I implemented a work around and it is working perfectly fine in my situation. For getting my date in my desired format, I decided to use two NSDateFormatter
with different locale and then I appended string from both the formatter to get desired results.
For getting month in three letter month, I made a NSDateFormatter
object
and set it's locale as [NSLocale currentLocale]
this gave me month name in three letter format.
For getting GMT, I made another NSDateFormtter
object and set it's locale as [NSLocale systemLocale]
and this gave me GMT format.
After this, I appended both the strings and I got desired results. Complete modified code given below:
-(void) datePickerAction: (UIDatePicker *)sender {
[self.datePicker setLocale:[NSLocale currentLocale]];
NSDateFormatter *dayMonthDate = [NSDateFormatter new];
[dayMonthDate setLocale:[NSLocale currentLocale]];
[dayMonthDate setDateFormat:@"EEE MMM dd HH:mm:ss"];
NSDateFormatter *gmtAndYearFormatter = [NSDateFormatter new];
[gmtAndYearFormatter setLocale:[NSLocale systemLocale]];
[gmtAndYearFormatter setDateFormat:@"zzzz yyyy"];
self.dateTextView.text = [NSString stringWithFormat:@"%@ %@",
[dayMonthDate stringFromDate:self.datePicker.date],
[gmtAndYearFormatter stringFromDate:self.datePicker.date]];
}
So, I got result as Tue Sep 29 11:58:29 GMT+05:30 2015
Upvotes: 1
Reputation: 331
It is better to create one Function so you can easily way to access and what you have to return.
e.g.
-(NSString *)getStringFromMonth:(NSString *)strMonth
{
NSInteger monthNum = [strMonth integerValue];
switch (monthNum)
{
case 1:
return @"Jan";
break;
case 2:
return @"Feb";
break;
case 3:
return @"Mar";
break;
case 4:
return @"Apr";
break;
case 5:
return @"May";
break;
case 6:
return @"Jun";
break;
case 7:
return @"Jul";
break;
case 8:
return @"Aug";
break;
case 9:
return @"Sep";
break;
case 10:
return @"Oct";
break;
case 11:
return @"Nov";
break;
case 12:
return @"Dec";
break;
default:
break;
}
return strMonth;
}
Upvotes: 1
Reputation: 16660
MMM
does not format a 3 letter abbreviation. You get an abbreviation, but what it is, depends on the locale. The official Unicode documentation has an example with a 4(!) letter abbreviation ("Sept"):
http://unicode.org/reports/tr35/tr35-6.html#Date%5FFormat%5FPatterns
So what you get depends on the locale. Since +systemLocale
delivers "no locale", …
Use the system locale when you don’t want any localizations.
… I suppose that NSDateFormatter
simply has no idea to create a month abbreviation without locale information. And even this would be possible, it would be pointless, because what should a localized form without locale should be? Swahili month names with indian abbreviations?
Likely you want to use +currentLocale
, which is the user setting on the device.
Upvotes: 1