2 daffodilgirl DaffodilGirl 于 2013.02.26 10:26 提问

ios:转换string到data

日期:"Mar 13, 2013 05:30pm"。我想转换成NSDate对象。

代码如下,但是失败了。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM:dd, YYYY hh:mm:aa"];
date=[dateFormatter dateFromString:dateString];

2个回答

g989_1314125
g989_1314125   2013.02.26 13:18
已采纳
NSString *stringDate = @"Mar 13, 2013 05:30pm";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MMM dd, yyyy hh:mmaa"];
NSDate *date=[dateFormatter dateFromString:stringDate];
NSLog(@"Date : %@",date);
redCoral_
redCoral_   2013.02.26 11:18
NSDateFormatter *dFormate = [[NSDateFormatter alloc] init];
[dFormate setDateFormat:@"MM dd, yyyy hh:mma"];
[dFormate setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];

NSString *dateStr = @"Mar 13, 2013 05:30pm";
NSDate *date = [dFormate dateFromString:dateStr];

NSLog(@"Date : %@", date);
Csdn user default icon
上传中...
上传图片
插入图片