-1

I am using the datepicker for picking the time but after sending to the server when I am retrieving back then its showing 5-6 hour difference.

Server hosted in USA.

So how I will do it accurately without any difference, User do request from any where.

Thanks,

Arun

Girish
  • 4,692
  • 4
  • 35
  • 55
San007
  • 762
  • 1
  • 9
  • 30

5 Answers5

1

UTC is standard time zone to be used. Following is the code to get date in UTC

+(NSString *)getCurrentTime{

    NSDate *date = [NSDate date];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];

    [dateFormat setDateFormat:@"yyyy-MM-ddHH:mm:ss"];

    [dateFormat setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];

    NSString *dateStr = [dateFormat stringFromDate:date];

    RELEASE_OBJECT(date)

    RELEASE_OBJECT(dateFormat)

    return dateStr;
}
DD_
  • 7,230
  • 11
  • 38
  • 59
Payal
  • 116
  • 3
0

If you are sending your iPhone default times to server and server is also sending the same time which you have send it then it will be problem of the converting your NSDate to NSString with NSDateFormatter with some different timezone.

[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];

Use above code when you using NSDateFormatter.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yashesh
  • 1,799
  • 1
  • 11
  • 29
0

Send the date with the timezone. For example:

NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy'-'MM'-'dd'T'HH':'mm':'ss'Z'"];
NSDate *date = [formatter dateFromString:dateString];

Will include the timezone and the server will be able to translate to its own timezone.

We use the ISO 8601 for better compatibility. There are also NSFormatter subclasses that to convert from ISO 8601 to NSDate and back (like this).

redent84
  • 18,901
  • 4
  • 62
  • 85
0

It is time zone issue. You can use [yourDate dateByAddingInterval:[NSTimeZone secondsFromGMT]]; .

Sadia
  • 462
  • 1
  • 5
  • 13
0

Please try to set the Locale for the time:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd MM yyyy hh:mm:ss"];
    [dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"] autorelease]];

     NSDate* sourceDate = [dateFormatter dateFromString:dateString];

    //Timezones
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];

    //Interval in Timezones
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;

    //converted date
    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] ;
    NSString *strFinalDate =  [dateFormatter stringFromDate:destinationDate];

    [dateFormatter release];

    [destinationDate release];


Meet
  • 4,904
  • 3
  • 24
  • 39