Nsdateformatter: Date According to Currentlocale, Without Year

NSDateFormatter: Date according to currentLocale, without Year

I think you need to take a look at:

+ (NSString *)dateFormatFromTemplate:(NSString *)template options:(NSUInteger)opts locale:(NSLocale *)locale

As per the docs:

Returns a localized date format string representing the given date format components arranged appropriately for the specified locale.
Return Value
A localized date format string representing the date format components given in template, arranged appropriately for the locale specified by locale.

The returned string may not contain exactly those components given in template, but may—for example—have locale-specific adjustments applied.

Discussion

Different locales have different conventions for the ordering of date components. You use this method to get an appropriate format string for a given set of components for a specified locale (typically you use the current locale—see currentLocale).

The following example shows the difference between the date formats for British and American English:

NSLocale *usLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
NSLocale *gbLocale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_GB"];

NSString *dateFormat;
// NOTE!!! I removed the 'y' from the example
NSString *dateComponents = @"MMMMd"; //@"yMMMMd";

dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:usLocale];
NSLog(@"Date format for %@: %@",
[usLocale displayNameForKey:NSLocaleIdentifier value:[usLocale localeIdentifier]], dateFormat);

dateFormat = [NSDateFormatter dateFormatFromTemplate:dateComponents options:0 locale:gbLocale];
NSLog(@"Date format for %@: %@",
[gbLocale displayNameForKey:NSLocaleIdentifier value:[gbLocale localeIdentifier]], dateFormat);

// Output:
// Date format for English (United States): MMMM d, y
// Date format for English (United Kingdom): d MMMM y

Extra code (add this to the code above):

// 
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
formatter.locale = gbLocale;
formatter.dateFormat = dateFormat;
NSLog(@"date: %@", [formatter stringFromDate: [NSDate date]]);

See here:
NSDateFormatter Class Reference

iOS Date Formatting without year, with correct localization

I believe this part of the documentation is the key (I mean if I was just to re-set the locale on the formatter without setting again the template, I would get what you describe in the first case):

func setLocalizedDateFormatFromTemplate(String)

Important: You should call this method only after setting the locale of
the receiver.

and sure enough, when you do this, you get correct formatting:

let dateFormatter = DateFormatter()
dateFormatter.locale = Locale(identifier: "en_US")
dateFormatter.setLocalizedDateFormatFromTemplate("MMM dd")
print(dateFormatter.string(from: Date())) // --> Jun 28

dateFormatter.locale = Locale(identifier: "fr_FR")
dateFormatter.setLocalizedDateFormatFromTemplate("MMM dd")
print(dateFormatter.string(from: Date())) // --> 28 juin

By the way, there is a really good session about formatters in WWDC'20

NSDateformatter setDateFormat according to currentLocale

Look at NSDateComponents to create an NSDate, then use NSDateFormatter to format it. NSDateFormatter uses the current locale to format dates, based on the format style (e.g.

NSDateFormatterMediumStyle).

NSDateFormatter printing current date in format M/yyyy hh:mm in any locale

If you want to format a date in a specific format, then why are you using dateFormatFromTemplate:? The purpose of that method is to give you a locale specific format from a general format.

Just use normal NSDateFormatter functionality:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"M/yyyy hh:mm"];
NSDate *now = [NSDate date];
NSString *formattedDate = [formatter stringFromDate:now];

Using NSDateFormatter on the iPhone, protect locale but lose date components

You could try something like:

//create a date formatter with standard locale, then:

// have to set a date style before dateFormat will give you a string back
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];

// read out the format string
NSString *format = [dateFormatter dateFormat];
format = [format stringByReplacingOccurrencesOfString:@"y" withString:@""];
[dateFormatter setDateFormat:format];

Kind of a hack, but it should work.

Edit: You may want to remove occurrences of the strings @"y," and @" y" first, in case you end up with some funky extra spaces or commas.



Related Topics



Leave a reply



Submit