-1

I am doing a prayer alarm app and i want to compare my current time with fetched time, but when i am getting my current time, it has some time difference always as shown;

       // getting today's date string

        NSDate *today = [NSDate date];
        NSDateFormatter *dateFormatterToday = [[NSDateFormatter alloc] init];
        dateFormatterToday.timeZone = [NSTimeZone localTimeZone];
        [dateFormatterToday setDateFormat:@"yyyy-MM-dd HH:mm a"];
        NSString *currentDateTimeString = [dateFormatterToday stringFromDate:today];

        // converting today's date string to NSDATE

        NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
        [dateFormatterTodayFinal setDateFormat:@"yyyy-MM-dd HH:mm a"];
        dateFormatterTodayFinal.timeZone = [NSTimeZone localTimeZone];
        NSDate *dateTodayFinal = [dateFormatterTodayFinal dateFromString:currentDateTimeString];  

Here currentDateTimeString, which is in string format showing my current time as :

2016-01-11 17:52 PM (which is correct one)

but dateTodayFinal, which is in Date format shows:

2016-01-11 07:22:00 +0000

I have tested with different timezones, but the issue persist, please help some one. Thank you.

Mumthezir VP
  • 6,251
  • 6
  • 28
  • 57
  • What's the reason for formatting the date as a string, then trying to turn the string back into a date? –  Jan 11 '16 at 12:56
  • It is not really clear what you are trying to achieve. The naming of `dateFormatterToday`and `dateFormatterTodayFinal` suggests it's not only about converting back and forth. – lukas_o Jan 12 '16 at 08:47

2 Answers2

0

The second example is missing a stringFromDate call so the description method is probably used which uses it's own format, not the dateFormatterToday formatter. Also missing are the printing calls so we can only guess.

Add:

NSString *dateTodayFinalTimeString = [dateFormatterToday stringFromDate:dateTodayFinal];
NSLog(@"dateTodayFinalTimeString: %@", dateTodayFinalTimeString);

Output:

dateTodayFinalTimeString: 2016-01-11 07:57 AM

zaph
  • 111,848
  • 21
  • 189
  • 228
  • Elaborate. The date is in an internal format in `NSDate` and no formatter changes that. Data formatters are used to control the conversion between string and `NSDate` representations. `NSDate` objects keep the date as the time interval relative to an absolute reference date (00:00:00 UTC on 1 January 2001). – zaph Jan 11 '16 at 13:09
0

NSDate doesn't have any information about the timeZone, when you hover over the NSDate in xCode it will show you the time is in UTC.

If you want to convert the time back and forth, this information (timezone) has to be in the string you want to parse as well and set the timezone back to UTC:

NSDate *today = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.timeZone = [NSTimeZone localTimeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
NSString *currentDateTimeString = [dateFormatter stringFromDate:today];

// converting today's date string to NSDATE
//    
//    NSDateFormatter *dateFormatterTodayFinal = [[NSDateFormatter alloc] init];
//    [dateFormatterTodayFinal setDateFormat:@"yyyy-MM-dd HH:mm:ss Z"];
dateFormatterToday.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
NSDate *dateTodayFinal = [dateFormatter dateFromString:currentDateTimeString];

Also have a look at the docs.

lukas_o
  • 3,776
  • 4
  • 34
  • 50