How is it possible to convert a string to an NSDate
on iOS?
Asked
Active
Viewed 7.8k times
69
6 Answers
197
NSString *dateStr = @"20100223";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
Hope this will help you.

Suresh Thayu
- 2,132
- 1
- 13
- 8
-
15found an article that explains all the format values here: http://www.alexcurylo.com/blog/2009/01/29/nsdateformatter-formatting/ – mtmurdock Jun 12 '12 at 19:18
-
Also note unicode.org's listing: http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns – Ben Flynn Mar 16 '13 at 23:48
-
Do you know why small letter yyyy and dd works and capital letter MM works...? – Rishabh Tayal Apr 11 '13 at 17:51
9
You'll want to take a look at NSDateFormatter. Determine the format of the date string and then use dateFromString:
to convert the string to an NSDate object.

Giao
- 14,725
- 2
- 24
- 18
5
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"dd/MM/YYYY"];
NSDate *date = [dateFormat dateFromString:dateStr];
list = [self getYear:date];
- (NSMutableArray *)getYear:(NSDate*)date
{
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
int year = [components year];
int month = [components month];
int day = [components day];
NSLog(@"%d",year);
NSMutableDictionary *dateDict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d", day], @"day", [NSString stringWithFormat:@"%d", month], @"month", [NSString stringWithFormat:@"%d", year], @"year", nil];
return dateDict;
}

Alex Zavatone
- 4,106
- 36
- 54

Niketan Rana
- 49
- 1
- 5
-
return type is NSMutableArray, but your returning NSMutableDictionary why? – kiran Apr 29 '18 at 21:46
3
My working version in Swift
func dateFor(timeStamp: String) -> NSDate
{
let formater = NSDateFormatter()
formater.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
return formater.dateFromString(timeStamp)!
}
func timeStampFor(date: NSDate) -> String
{
let dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "HH:mm:ss:SSS - MMM dd, yyyy"
return dateFormatter.stringFromDate(date)
}

Raphael Souza
- 541
- 4
- 11
2
A simple approach even if you have sqlite DB:
// NSTimeInterval is basically typedef of double so you can receive it as double also.
NSTimeInterval *current = [[NSDate date] timeIntervalSince1970];
[DB addToDB:current];
//retrieve the date from DB
NSDate *retrievedDateItem = [[NSDate alloc] initWithTimeIntervalSince1970:[getDBValueAsDouble]]; //convert it from double (NSTimeInterval) to NSDate
// Set the style
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterMediumStyle];
// Converted to string
NSString *convertedDate = [dateFormatter stringFromDate:retrievedDateItem];
The output for this example:
Aug 3, 2015, 12:27:50 PM

OhadM
- 4,687
- 1
- 47
- 57
1
+(NSDate*)str2date:(NSString*)dateStr{
if ([dateStr isKindOfClass:[NSDate class]]) {
return (NSDate*)dateStr;
}
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSDate *date = [dateFormat dateFromString:dateStr];
return date;
}

Gank
- 4,507
- 4
- 49
- 45