Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use NSDateFormatter to see "Today" string

Tags:

apple use it in the messages app for today messages Today 11:45 AM Yesterday 11:45 AM

i see it in the apple developer site

To specify a custom fixed format for a date formatter, you use setDateFormat:. The format string uses the format patterns from the Unicode Technical Standard #35. The version of the standard varies with release of the operating system:

Calendar Fields

fields ( alias | (field*, special*)) > field ( alias | (displayName?, relative*, special*)) >

Translations may be supplied for names of calendar fields (elements of a calendar, such as Day, Month, Year, Hour, and so on), and for relative values for those fields (for example, the day with relative value -1 is "Yesterday"). Where there is not a convenient, customary word or phrase in a particular language for a relative value, it should be omitted.

how to use it in Xcode?

like image 857
user301212 Avatar asked Sep 08 '13 09:09

user301212


People also ask

Is NSDateFormatter thread safe?

Thread Safety On earlier versions of the operating system, or when using the legacy formatter behavior or running in 32-bit in macOS, NSDateFormatter is not thread safe, and you therefore must not mutate a date formatter simultaneously from multiple threads.

What is NSDate?

The NSDate class provides methods for comparing dates, calculating the time interval between two dates, and creating a new date from a time interval relative to another date.

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

use – setDoesRelativeDateFormatting:

NSDateFormatter *dateFormatter = [NSDateFormatter new];  dateFormatter.dateStyle = NSDateFormatterShortStyle; dateFormatter.timeStyle = NSDateFormatterShortStyle;  dateFormatter.doesRelativeDateFormatting = YES;  NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]); 

this logs Today, 11:30 AM on en_US locales.

like image 87
Matthias Bauch Avatar answered Sep 28 '22 09:09

Matthias Bauch