Date Formatter issue in ObjC [duplicate]

为君一笑 提交于 2019-12-25 03:02:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!