1

我只是使用下面的代码将字符串转换为日期。代码运行完美,但有时它不会返回日期。任何想法 ??

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSDate *myDate  = [[NSDate alloc] init];
myDate = [dateFormatter dateFromString:strTime];

谢谢,

4

1 回答 1

0

当您想将字符串转换为日期时,只需使用您的字符串格式的波纹管方法

-(NSDate *)convertStringToDate:(NSString *) date {
     NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
    NSDate *nowDate = [[[NSDate alloc] init] autorelease];
    [formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"]; // use formate of date which you got in string date..
    // NSLog(@"date============================>>>>>>>>>>>>>>> : %@", date);
     date = [date stringByReplacingOccurrencesOfString:@"+0000" withString:@""];
    nowDate = [formatter dateFromString:date];
    return nowDate;
}
于 2012-11-08T10:00:32.420 回答