First, I assume you meant ddMMyyyy HH:mm:ss
[dateFormat setDateFormat:@" dMMyyyy HH:mm:ss"];
Second, it will depend on what your Currentdate
is set to. It must match your NSDateForatter or it will not work. For example
NSString *Currentdate = @"2014-01-01 01:00:00";
NSString *Currentdate2 = @"01012014 01:00:00";
NSDateFormatter *dateFormat=[NSDateFormatter new];
[dateFormat setDateFormat:@"ddMMyyyy HH:mm:ss"];
NSDate *date = [dateFormat dateFromString:Currentdate];
NSLog(@"Currentdate %@",date);
Will log the follow
2014-01-22 23:14:27.958 TableViewCellWithAutoLayout[94829:a0b] Currentdate (null)
2014-01-22 23:14:27.959 TableViewCellWithAutoLayout[94829:a0b] Currentdate2 = 2014-01-01 07:00:00 +0000
Why is the first date null and the second date valid? It's because the format was correct in the second date, but not the right format (for this NSDateFormatter) in the first.
Also this line
NSDate *today = [dateFormat dateFromString:[dateFormat stringFromDate:[NSDate date]]];
Is the same as this line
NSDate *today = [NSDate date];
You are just converting from a NSDate to string to NSDate.
To convert the date from the format @"Thur, 2 Jan 2014 19:40:18 GMT"
I'm going to strip the GTM
and the Thur,
from the string and use the format d MMM yyyy HH:mm:ss
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[dateFormatter setDateFormat:@"d MMM yyyy HH:mm:ss"];
NSString *dateString = @"Thur, 2 Jan 2014 19:40:18 GMT";
dateString = [dateString stringByReplacingOccurrencesOfString:@"GMT" withString:@""];
NSRange range = [dateString rangeOfString:@", " options:NSBackwardsSearch range:NSMakeRange(0, [dateString length]-1)];
NSLog(@"range.location: %lu", range.location);
dateString = [dateString substringFromIndex:range.location+1];
NSDate *date = [dateFormatter dateFromString:dateString];
NSLog(@"date = %@",date);