Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS : Date format - 20-Sep-2012

Tags:

date

ios

iphone

I want to display the date in the format "20-Sep-2012" from the string "2012-11-22 10:19:04". How can i do this? Is there any in-built method for iOS?

like image 722
Dev Avatar asked Nov 22 '12 04:11

Dev


People also ask

What date format does swift use?

era: G (AD), GGGG (Anno Domini) year: y (2018), yy (18), yyyy (2018) month: M, MM, MMM, MMMM, MMMMM. day of month: d, dd.

How do I change the format of one date format in Swift?

Convert ISO8601 To Date In Swift import Foundation let isoDate = "2020-01-22T11:22:00+0000" let dateFormatter = DateFormatter() dateFormatter. locale = Locale(identifier: "en_US_POSIX") dateFormatter. dateFormat = "yyyy-MM-dd'T'HH:mm:ssZ" if let date = dateFormatter. date(from: isoDate) { // do something with date... }


2 Answers

NSString *myString = @"2012-11-22 10:19:04";
NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";     
NSDate *yourDate = [dateFormatter dateFromString:myString];
dateFormatter.dateFormat = @"dd-MMM-yyyy";
NSLog(@"%@",[dateFormatter stringFromDate:yourDate]);

your log will print like this. 22-Nov-2012

like image 180
Dinesh Raja Avatar answered Sep 26 '22 00:09

Dinesh Raja


Try this,

NSString *originalDateString = @"2012-11-22 10:19:04";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//2012-11-22 10:19:04

Then use it to parse the date string as,

NSDate *date = [dateFormatter dateFromString:originalDateString];

You can create a new dateformatter to print in the new format or just reuse the existing one as,

[dateFormatter setDateFormat:@"dd-MMM-yyyy"];//22-Nov-2012
NSString *formattedDateString = [dateFormatter stringFromDate:date];
NSLog(@"Date in new format is %@", formattedDateString);
like image 32
iDev Avatar answered Sep 24 '22 00:09

iDev