Search code examples
iosobjective-ciphonexcode7nsdateformatter

NSDateFormatter is not working with iOS


I developed an app before one month at that time it is working fine but now i am going to change some dates but it returning nil

 NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"dd-MM-yyyy HH:mm"];
    NSString *str = [NSString stringWithFormat:@"%@",[NSDate date]];
    NSDate *changeDate2=[formatter2 dateFromString:str];

this code which was working . but now changeDate2 is nil .


Solution

  • You need use your NSDateFormatter for convert [NSDate date] to string. Modify your code to below

    NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
    [formatter2 setDateFormat:@"dd-MM-yyyy HH:mm"];
    NSString *str = [formatter2 stringFromDate:[NSDate date]];
    NSDate *changeDate2=[formatter2 dateFromString:str];