How can i convert the string to date and date from string?

拥有回忆 提交于 2019-12-06 10:47:27

You are using wrong format for date format and also there is no need to create another instance of NSDateFormatter -

    NSString *inputString=@"15 February 2012 17:05"; 
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"]; 
    NSDate *dateVal = [dateFormat dateFromString:inputString];

    [dateFormat setDateFormat:@"MM/dd/yyyy HH:mm"];
    NSString *outputString = [dateFormat stringFromDate:dateVal];

The date format is incorrect, use:

[dateFormat setDateFormat:@"dd MMMM yyyy HH:mm"];

Capital "YYYY" is for use in "Week of Year" based calendars.

See: unicode Date_Format_Patterns

BTW, an NSLog of dateVal would have shown that the problem was on the input formatting.

Does your locale correspond to the format you are trying to parse? By default NSDateFormatter uses current locale.

The input date format should be:

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