How to convert datetime format to NSString?

I have a value from xml 2009-11-23T05:24:41.000Z.

Now I want to display the line as follows: Wed, Nov 4, 2009 8:00 PM - Wed, Nov 4, 2009 9:00 PM

How is this possible?

+3
source share
2 answers

You need to do something like this:

NSDateFormatter * inputFormatter = [ [ [ NSDateFormatter alloc ] init ] autorelease ];
NSDateFormatter * outputFormatter = [ [ [ NSDateFormatter alloc ] init ] autorelease ];
[ inputFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'.000Z'" ];
[ outputFormatter setDateFormat:@"EEE, MMM d, yyyy h:mm a" ]; 

NSString * inputString = @"2009-11-23T05:24:41.000Z";
NSDate * inputDate = [ inputFormatter dateFromString:inputString ];
NSString * outputString = [ outputFormatter stringFromDate:inputDate ];

// outputString = @"Mon, Nov 23, 2009 05:24 AM"

For more information on customization, NSDateFormattersee the Unicode Date Format Template Guide .

+2
source

Use methods NSDateFormatter -dateFromString:and -stringFromDate:.

+3
source

Source: https://habr.com/ru/post/1728715/


All Articles