How to convert a string to date? I know we can use dateFormatter to do this but I am stuck in between. I have format 0512 which should be converted to May 2012.
Asked
Active
Viewed 124 times
4 Answers
3
Try this,
NSString *finalDate = @"0512";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMyy"];
NSDate *date = [dateFormatter dateFromString:finalDate];
[dateFormatter setDateFormat:@"MMM yyyy"];
NSLog(@"%@",[dateFormatter stringFromDate:date]);

βhargavḯ
- 9,786
- 1
- 37
- 59
-
:thanx a lot, u saved me. – Shantanu Feb 27 '13 at 06:11
-
Be wary I've used this and: 12/49 will give you Dec 2049 and then 01/50 will return Jan 1950 – Nigel Oct 13 '15 at 18:28
2
Try this
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"MMyy"];
NSDate *dateObj= [dateFormatter dateFromString:@"0512"];
NSLog(@"%@",dateObj);
[dateFormatter setDateFormat:@"EEEE MMMM d, YYYY"];
NSLog(@"%@",[dateFormatter stringFromDate:dateObj]);

Lithu T.V
- 19,955
- 12
- 56
- 101
1
Try this code may be helped you..
NSString *dateStr = @"0512";
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMyy"];
NSDate *date = [dateFormat dateFromString:dateStr];
// Convert date object to desired output format
[dateFormat setDateFormat:@"MMMM YYYY"];
dateStr = [dateFormat stringFromDate:date];
[dateFormat release];
NSLog(@"%@",dateStr);

Vikas S Singh
- 1,748
- 1
- 14
- 29
0
What is so difficult about it? 1. Take out first 2 characters of string 2. Put if else/ convert string to int and use switch to make up month 3. Take the next 2 characters, add them to 2000 and you get year
Now user iOS functions to achieve this :)

Sagrian
- 1,048
- 2
- 11
- 29