Search code examples
iosobjective-cnsdatensdateformatter

Removing and reformatting of NSDate is not working?


I am trying to reformat this NSdate (2016-02-11 01:00:00 +0000 ) into this (11-02-2016).

I have used below code and its returning null.

//newDate format is 2016-02-11 01:00:00 +0000
NSString *dateString = [[self dateFormatter] stringFromDate:newDate];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString:dateString];

// Convert date object into desired format
[dateFormatter setDateFormat:@"dd-MM-yyyy"];
NSString *newDateString = [dateFormatter stringFromDate:date];
NSLog(@"people%@",newDateString); //printing null

Solution

  • +0000 it the time zone definition. You should parse it with Z

    try this

    [dateformat setDateFormat:@"yyyy-MM-dd HH:mm:ssZZZZ"];
    

    else if your date is current date use

    [dateformat setDateFormat:@"yyyy-MM-dd HH:mm:ss"];