我只是使用下面的代码将字符串转换为日期。 代码工作正常,但有时它不会返回日期。 有什么想法??
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSDate *myDate = [[NSDate alloc] init];
myDate = [dateFormatter dateFromString:strTime];
谢谢,
答案 0 :(得分:0)
如果要将字符串转换为日期,请使用带有字符串格式的bellow方法
-(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;
}