问题
I need to convert a date string "2015-03-10 00:00:00.000"(string from web service) to NSDate. I have used code like this
NSDateFormatter *fromDateFormat = [[NSDateFormatter alloc] init];
[fromDateFormat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *outputDate = [fromDateFormat dateFromString:@"2015-03-10 00:00:00.000"];
outputDate is nil. I tried without last triple zeros, and it is working. but I can't remove that zeros, because it is from a service.
回答1:
If you want convert string to date using This function
NSDate *current=[self convertStringToDate:data.strCreateDate formate:@"yyyy-MM-dd HH:mm:ss"];
Put this method in your projects
- (NSDate *)convertStringToDate: (NSString *)date formate:(NSString *)formate {
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:formate];
return [dateFormat dateFromString:date];
}
This is helpfult to you..
来源:https://stackoverflow.com/questions/33315678/date-formatter-issue-in-objc