convert NSString to NSDate [duplicate]
Possible Duplicate:
开发者_运维知识库 NSString to NSDate
Hi I want to convert the string "OCT 1, 2011 18:30
" into NSDate
format 2011-05-13 19:00:00 +0800.
I have tried with the many formatters but what I am getting is a null value each time.
Can any body help me on this?
You can try this code that will help you because I run successfully.
NSString *dateStr = @"OCT 1, 2011 18:30";
NSDateFormatter *dtF = [[NSDateFormatter alloc] init];
[dtF setDateFormat:@"LLL d, yyyy HH:mm"];
NSDate *d = [dtF dateFromString:dateStr];
NSDateFormatter *dateFormatStr = [[NSDateFormatter alloc] init];
[dateFormatStr setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSDate *st = (NSDate *)[dateFormatStr stringFromDate:d];
NSLog(@"%@",st);
try this
NSDateFormatter *datefor = [[[NSDateFormatter alloc] init] autorelease];
[datefor setDateFormat:@"MMM dd, yyyy HH:mm"];
NSDate *date = [datefor dateFromString:@"OCT 1, 2011 18:30"];
[datefor setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *str= [[NSString alloc] initWithString:[datefor stringFromDate:date]];
NSDate *date_fianl = [datefor dateFromString:str];
[str release];
NSLog(@"%@",date_fianl);//it will print 2011-10-01 01:00:00 +0000
精彩评论