3

I want to convert my Local date ([NSDate date]) to GMT to create a JSON string (/Date(1324435876019-0000)/).

My code works fine when i set my clock to EST time zone when i change my timezone to PST my code still acts as EST. Can you please tell me what is the problem is following code?

        NSDate *localDate = [NSDate date];
    NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMT]; // You could also use the systemTimeZone method
    NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
    NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
    NSString *time = [NSString stringWithFormat:@"/Date(%@-0000)/", [NSNumber numberWithLongLong:[gmtDate timeIntervalSince1970] * 1000]];
AAV
  • 3,785
  • 8
  • 32
  • 59
  • Current time is 2011-12-20 05:15:00 +0000 But if i NSLog gmtDate i am getting out put as 1) ESt tim zone: 2011-12-21 03:15:00 +0000 2) PSTtime zone :2011-12-21 06:15:00 +0000. Which is wrong. I think i am not getting right GMT date. What you think ? – AAV Dec 20 '11 at 22:18

2 Answers2

7

You can get the epoch GMT time by simply calling timeIntervalSince1970.

NSString *time = [NSString stringWithFormat:@"/Date(%lld-0000)/", 
                     (long long)([[NSDate date] timeIntervalSince1970] * 1000)];
Joe
  • 56,979
  • 9
  • 128
  • 135
  • First thing timeIntervalSince1970 will give you in second and we need to pass millisecond for JSON. I implemented this logic first but i was not able to find right answer. – AAV Dec 21 '11 at 16:44
  • Updated answer. You do not need to do any timezone calculations to get the GMT time, that one liner is all you need. – Joe Dec 21 '11 at 16:55
  • @AmitVyawahare: timeIntervalSince1970 uses seconds as its units but returns an NSTimeInterval, which is a double, with quite a bit of fractional precision. So Joe's solution is correct and should give you millisecond resolution. – Tommy Dec 21 '11 at 17:28
0

@joe and @tommy you are right. Even i implemneted the same way as you mentioned. Problem was in server database. I found a hack. I just need to add 18000 (5 hr) second to the current local date and i am good. So code is

        NSDate *localDate = [NSDate date];
    NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] + 18000;
    NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];

    NSString *time = [NSString stringWithFormat:@"/Date(%@-0000)/", [NSNumber numberWithLongLong:[gmtDate timeIntervalSince1970] * 1000]];
AAV
  • 3,785
  • 8
  • 32
  • 59
  • That is not really a good solution (as you know since you used the word hack), do not forget that there is `EST` and `EDT`. Your server just needs to display the time back in the correct timezone, maybe the solution is to change the "-0000" to "-0500"(EST)/"-0400"(EDT) instead. – Joe Dec 21 '11 at 21:57
  • You are right hack is not good but till the point server is not fixed i have to use same crapy code. Thank you, --AV – AAV Dec 21 '11 at 22:32