tags:

views:

41

answers:

2

Hi,

I have a stringdate 16-MAY-2010 23:04:44 which i need to convert to gmt time zone that is the out put required is 17-May-2010 12:03:03.

I used date formatters to convert but the result i am getting is not in the format i required.I am sending the code please let me know if i am doing correct or not

Here is the code:

NSString *timeStamp = [format stringFromDate:[NSDate date]];
NSString *output = [timeConv dateStringFromString:timeStamp];

- (NSString *)dateStringFromString:(NSString *)sourceString
{     
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
    [dateFormatter setDateFormat:@"dd-MMMM-yyyy HH:MM:ss"];
    NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
    [dateFormatter setTimeZone:gmt];
    NSDate *date = [dateFormatter dateFromString:sourceString];

    return [dateFormatter stringFromDate:date];
} 

The out put i am getting is the same old time without conversion. so please let me know the correct solution.

+1  A: 

I know no cocoa whatsoever, but if I'm understanding your code correctly, you're never telling the parser that your original string is not GMT, but rather GMT-1.

Based on your own code (and again, with no knowledge of cocoa), this is what I believe you need to do:

NSTimeZone *gmtminusone = [NSTimeZone timeZoneForSecondsFromGMT:-3600.0];
[dateFormatter setTimeZone:gmtminusone];
NSDate *date = [dateFormatter dateFromString:sourceString];
NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
return [dateFormatter stringFromDate:date];

If it doesn't work right away, maybe it gives you the idea of what to do:

  1. Parse the string with a date formatter set to GMT-1, to output a date.
  2. Parse the date with a date formatter set to GMT, to output a string.
Tomas Lycken
“GMT-1” isn't a time zone abbreviation. The questioner needs to specify whatever time zone it's in directly, either by name or by offset.
Peter Hosey
@Peter, As I said, I have no knowledge of cocoa whatsoever - that includes which time zone abbreviations are available. But given my example, the OP is hopefully able to figure out what to do anyway. If you know cocoa, and know what that line should be substituted to do the same thing, please feel free to edit my post.
Tomas Lycken
@Tomas Thanks Thomas i got the solution
Kishore
+1  A: 

The date string didn't include a time zone specification, so the date formatter relied on you to tell it what time zone it's in.

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSDate *date = [dateFormatter dateFromString:sourceString];

You told it that the input string was in GMT.

You need to tell the date formatter what time zone the date is in. Once you've done that, you can tell the formatter to convert the string to a date (dateFromString:), and it will interpret the string as representing a date in that time zone.

Then, once you have the date so interpreted, switch the formatter's time zone to GMT and have it output the string (stringFromDate:). That string will represent the date converted to GMT.

Peter Hosey