0

I have ios application with datetimepicker, and i am store the date in to database with "date" type. so it will store some number

date come from database and display in NSString like this : 2012-06-25 06:45:06 +0000

This is will give me GMT 0 and i want this time to GMT +5:30 and display it in to texlfield.


strTime = @"2012-06-25 06:45:06 +0000";// thats comes form database like this.

NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; [dateFormat setDateFormat:@"yyyy-MM-dd hh:mm"]; NSDate *myDate = [dateFormat dateFromString:strTime]; NSString *strDate = [dateFormat stringFromDate:myDate]; NSLog(@"%@", strDate); txtTime.text = strDate;

the result will be (null) i want to display the time like 2012-06-25 12:15:06 (with gmt +5:30)

1 Answers1

1

You will need to set the time zone, like following

NSString *strDate = @"2012-06-25 06:45:06 +0000";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss z"];
int secondsFromGMT = 5.5 * 3600;
[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:secondsFromGMT]];

NSDate *date = [formatter dateFromString:strDate];
NSString *strWithTimeZone = [formatter stringFromDate:date];
NSLog(strWithTimeZone); //2012-06-25 12:15:06 GMT+05:30
Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56