0
-(NSTimeInterval)convertStringToDate:(NSString *) date {

    NSString *dateString = date;
    NSLog(@"dateString = %@", dateString);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
    [dateFormatter setLocale:[NSLocale currentLocale]];
    NSDate *date1 = [[NSDate alloc] init];

    date1 = [dateFormatter dateFromString:dateString];
    NSLog(@"dateFromString = %@", date1);

    NSString *displayDate = [dateFormatter stringFromDate:date1];
    NSLog(@"displayDate = %@", displayDate);

    return [date1 timeIntervalSince1970];
}

Why I am getting NSTimeInterval with wrong timezone?

S.J
  • 3,063
  • 3
  • 33
  • 66
  • `NSTimeInterval` doesn't have a time zone. It's a length of time. An hour in Asia is the same as an hour in America. How does the number you're getting differ from the number you're expecting? – Tommy Oct 31 '14 at 16:59
  • @Tommy i am getting 5 hours back difference in date1 which I am trying to convert in NSTimeInterval. – S.J Oct 31 '14 at 17:07
  • @ArtFeel Any possible way that without adding GMT app can work properly in any timezone? – S.J Oct 31 '14 at 17:17
  • 1
    You're not getting a time 5 hours different in `date1`, you're choosing to log `date1` in GMT. That has no bearing whatsoever on what's contained within `date1`. All the time calculations are 100% correct and time zone independent. You've just made an inaccurate guess about how logging works. – Tommy Oct 31 '14 at 18:21

2 Answers2

0

You need to read up on the internal representation of NSDates. An NSDate is saved as the number seconds since midnight on 1 Jan, 1984 GMT (The Mac OS X "epoch date") . It represents an instant in time anywhere on the earth, but using a date in GMT as it's "zero date". To display it, you need to convert it to your local time zone.

NSDate has a couple of methods to convert a date to a number: timeIntervalSince1970, which converts an NSDate to the internet standard, which is the number of seconds since Midnight 1 Jan 1970 (The UNIX "epoch date"), and timeIntervalSinceReferenceDate, which converts to the number seconds since the Mac Epoch date.

If you display a date in NSLog:

NSLog(@"Date = %@", someNSDate); 

It will be displayed in GMT.

Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • Any example will be helpful Thank you. – S.J Oct 31 '14 at 17:55
  • I want accurate NSTimeInterval which I am not getting when converting using the above code :(. – S.J Oct 31 '14 at 17:56
  • How can I get accurate NSTimeInterval using above code? when I log string all is fine and when I convert it into date all is wrong. Why after converting the date goes mad? How can I get accurate date? – S.J Oct 31 '14 at 18:09
0

Honestly, it's unclear what you're asking and my best guess is that you just don't understand the classes at play. I've annotated your code in the hope of aiding your comprehension.

Key point: NSDate does not have a time zone. It's an opaque time stamp.

-(NSTimeInterval)convertStringToDate:(NSString *) date {

    // log the input string
    NSString *dateString = date;
    NSLog(@"dateString = %@", dateString);

    // create an object that can apply a locale and a time zone in order to
    // convert an NSDate to an NSString and vice versa
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"dd-MM-yyyy hh:mm a"];
    [dateFormatter setLocale:[NSLocale currentLocale]];


    // get a date that represents exactly now, for no reason as it's about
    // to be thrown away
    NSDate *date1 = [[NSDate alloc] init];

    // convert to the NSDate that represents the given string. 
    date1 = [dateFormatter dateFromString:dateString];

    // log the converted date. BECAUSE NSDATE DOES NOT HAVE A TIME ZONE,
    // it will arbitrarily be displayed in UTC. Because it has to be
    // displayed in something
    NSLog(@"dateFromString = %@", date1);

    // convert date1 back into a printable date; this will again apply
    // a time zone and locale
    NSString *displayDate = [dateFormatter stringFromDate:date1];
    NSLog(@"displayDate = %@", displayDate);

    // query the date for "The interval between the date object and 
    // January 1, 1970 at 12:00 a.m. GMT."; return that
    return [date1 timeIntervalSince1970];
}
Tommy
  • 99,986
  • 12
  • 185
  • 204
  • the parameter I am passing is string concatenation of date and time like "30-10-2014 11:31 am", dateFromString = 2014-10-31 18:31:00 +0000 and displayDate = 31-10-2014 11:31 pm. As you can see dateFromString is not accurate so when I am converting it to NSTimeInterval I am getting wrong result. How can I get accurate date without time + or -. – S.J Oct 31 '14 at 18:44
  • I want to pass the NSTimeInterval to UILocalNotification so it can trigger at accurate time. – S.J Oct 31 '14 at 18:46
  • `UILocalNotification` takes a date, not a time interval. Just give it `date1`. – Tommy Oct 31 '14 at 18:55