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?
era: G (AD), GGGG (Anno Domini) year: y (2018), yy (18), yyyy (2018) month: M, MM, MMM, MMMM, MMMMM. day of month: d, dd.
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... }
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With