Change Date Format on iPhone

I have been trying this since the last two hours and finally left it for you guys: P

I need to convert this line Mon, 14 May 2012, 12:00:55 +0200 to date format dd/mm/yyyy hh:ss .

Any help in achieving the goal will be truly appreciated.

What i tried

I tried using NSDateFormatter, but I can not determine the exact format of the specified date. This is [dateFormatter setDateFormat:@"EEEddMM,yyyy HH:mm:ss"]; as I tried many other formats too

For instance:

  NSString *finalDate = @"Tue, 29 May 2012, 14:24:56 +0200"; NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"EEEddMM,yyyy HH:mm:ss"]; NSDate *date = [dateFormatter dateFromString:finalDate]; 

Here date alwasys comes as nil

+4
source share
3 answers

The most important part of date formatting is often forgotten; specify the NSDateFormatter input language:

 NSDateFormatter *dateFormatter = [NSDateFormatter new]; [dateFormatter setDateFormat:@"EEE, dd MMMM yyyy, HH:mm:ss Z"]; dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"EN"]; NSDate *date = [dateFormatter dateFromString:@"Mon, 14 May 2012, 12:00:55 +0200"]; NSLog(@"date: %@", date); 

I checked the output: date: 2012-05-14 10:00:55 +0000

Keep in mind that HH in date formatting for 24hr .

Now, I would suggest not using a fixed output scheme, but using one of NSDateFormatterStyle :

 [dateFormatter setDateStyle:NSDateFormatterShortStyle]; [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; NSString *dateString = [dateFormatter stringFromDate:date]; NSLog(@"date: %@", dateString); 

which in American English displays: 5/14/12 12:00 PM


This code is valid for ARC unless you use ARC add autorelease in NSLocale and release NSDateFormatter after you are done with it.

+4
source
 NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init]; [inputFormatter setDateFormat:@"dd/mm/yyyy hh:ss"]; NSString *formatterDate = [inputFormatter stringFromDate:inDate]; [inputFormatter release]; 
0
source

get information about all date formats my link is on the blog http://parasjoshi3.blogspot.in/2012/01/date-formate-info-for-iphone-sdk.html

as well as a small feature below.

 -(NSString *) dateInFormat:(NSString*) stringFormat { char buffer[80]; const char *format = [stringFormat UTF8String]; time_t rawtime; struct tm * timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); strftime(buffer, 80, format, timeinfo); return [NSString stringWithCString:buffer encoding:NSUTF8StringEncoding]; } 

//////// Like .......

 NSString *mydate = [self dateInFormat:@"%Y%m%d-%H%M%S"]; 

hope this helps you ....

0
source

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


All Articles