Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDateFormatter: Date according to currentLocale, without Year

This can't be too difficult..

I want to display a date without the year. For example: "Aug, 2nd" (USA) or "02.08." (GERMANY) It must work for a bunch of other locales as well.

My only idea so far is to do a normal format with year, and then remove the year-portion from the generated string.

like image 944
scrrr Avatar asked Sep 12 '13 14:09

scrrr


People also ask

What is Dateformatter?

A formatter that converts between dates and their textual representations.

What is en_US_POSIX?

In most cases the best locale to choose is "en_US_POSIX", a locale that's specifically designed to yield US English results regardless of both user and system preferences.

How do I change the date format in Swiftui?

Your dateFormat in ListViewRow is E, dd MMM yyyy , whereas your date is in format E, dd MMM yyyy HH:mm:ss z . You must convert your date using this dateFormat. You can then use the date object to get a string in your desired format.


1 Answers

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

like image 120
Joride Avatar answered Sep 21 '22 13:09

Joride