0

I want to convert GMT Time which obtained from web service. I am getting the object like this

dateTime: {
           date: 23
           day: 3
           hours: 6
           minutes: 13
           month: 8
           nanos: 0
           seconds: 0
           time: 1442988780000
           timezoneOffset: 0
           year: 115
          }

I don't have idea how to convert this object to local phone time.

TechSavy
  • 797
  • 2
  • 8
  • 22

2 Answers2

2

You can use NSDateComponents and NSDate. See Apple's Date and Time Programming Guide

Quanlong
  • 24,028
  • 16
  • 69
  • 79
1

Step1- Convert data which you are getting in Dictionary or Array Step2- Access The dictionary by key-value pair. 'Step3-' Refer NSDate from apple doc

also try this-

NSString *dateStr = @"2012-07-16 07:33:01";
NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init]; 
[dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; 
NSDate *date = [dateFormatter1 dateFromString:dateStr];
NSLog(@"date : %@",date);

NSTimeZone *currentTimeZone = [NSTimeZone localTimeZone];
NSTimeZone *utcTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:date];
NSInteger gmtOffset = [utcTimeZone secondsFromGMTForDate:date];
NSTimeInterval gmtInterval = currentGMTOffset - gmtOffset;

NSDate *destinationDate = [[[NSDate alloc] initWithTimeInterval:gmtInterval sinceDate:date] autorelease];

NSDateFormatter *dateFormatters = [[NSDateFormatter alloc] init];
[dateFormatters setDateFormat:@"dd-MMM-yyyy hh:mm"];
[dateFormatters setDateStyle:NSDateFormatterShortStyle];
[dateFormatters setTimeStyle:NSDateFormatterShortStyle];
[dateFormatters setDoesRelativeDateFormatting:YES];
[dateFormatters setTimeZone:[NSTimeZone systemTimeZone]];  
dateStr = [dateFormatters stringFromDate: destinationDate];
NSLog(@"DateString : %@", dateStr);
Saheb Singh
  • 555
  • 4
  • 18