To convert from a localDate to GMT:
To convert from a GMT date to a localDate:
-(NSString *)localToGMT{
NSDate *currDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSString *dateString = [dateFormatter stringFromDate:currDate];
NSDate *localDate = [dateFormatter dateFromString:dateString];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:localDate];
NSDate *gmtDate = [localDate dateByAddingTimeInterval:-timeZoneOffset]; // NOTE the "-" sign!
NSString *gmtdateString = [dateFormatter stringFromDate:gmtDate];
[dateFormatter release];
return gmtdateString;
}
To convert from a GMT date to a localDate:
-(NSString *)GMTToLocal:(NSString *)GMTdateString{
NSDateFormatter *format = [[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *gmtDate = [format dateFromString:GMTdateString];
NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMTForDate:gmtDate];
NSDate *localDate = [gmtDate dateByAddingTimeInterval:timeZoneOffset];
[format setDateFormat:@"hh:mm:ss a, yyyy-MM-dd "];
NSString *localDateString = [format stringFromDate:localDate];
[format release];
return localDateString;
}
Help Link :
http://stackoverflow.com/questions/1862905/nsdate-convert-date-to-gmt/1863042#1863042
Comments
Post a Comment