dateFormat = "yyyy-MM-dd'T'HH:mm:ss" let oldDate = olDateFormatter. date(from: inputDate) let convertDateFormatter = DateFormatter() convertDateFormatter. dateFormat = "MMM dd yyyy h:mm a" return convertDateFormatter. string(from: oldDate!) }
Date() function in R Language is used to convert a string into date format.
Take a look at the class reference for NSDateFormatter. You use it like this:
NSString *dateStr = @"Tue, 25 May 2010 12:53:58 +0000";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EE, d LLLL yyyy HH:mm:ss Z"];
NSDate *date = [dateFormat dateFromString:dateStr];
[dateFormat release];
For more information on how to customize that NSDateFormatter, try this reference guide.
EDIT:
Just so you know, this is going to parse the full month name. If you want three letter month names, use LLL
instead of LLLL
.
-(NSString *)dateToFormatedDate:(NSString *)dateStr {
NSString *finalDate = @"2014-10-15";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormatter dateFromString:dateStr];
[dateFormatter setDateFormat:@"EE, d MMM, YYYY"];
return [dateFormatter stringFromDate:date];
}
If you are storing dates in one of iOS' styles, it's far easier and less error prone to use this method:
// Define a date formatter for storage, full style for more flexibility
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterFullStyle];
[dateFormatter setTimeStyle:NSDateFormatterFullStyle];
// Use today as an example
NSDate *today = [NSDate date];
// Format to a string using predefined styles
NSString *dateString = [dateFormatter stringFromDate:today];
// Format back to a date using the same styles
NSDate *todayFromString = [dateFormatter dateFromString:dateString];
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