Reputation: 87
I am facing problem while converting given date into system time zone in iOS.
Source date: 2013-03-18 03:54:18 // its in AM format Destination date should be : 2013-03-18 03:30:15 //its in PM format.
How can I get this??
I have tried below code but getting wrong data and difference too.
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
NSLog(@"Post date::%@",postDate);
NSDate *utc = [fmt dateFromString:postDate];
fmt.timeZone = [NSTimeZone systemTimeZone];
NSString *local = [fmt stringFromDate:utc];
NSLog(@" local %@", local);
NSDate *date1 = [NSDate date];
NSString *currentDateString = [fmt stringFromDate:date1];
NSDate *currentDate = [fmt dateFromString:currentDateString];
NSUInteger flags = NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit;
NSDateComponents *difference = [[NSCalendar currentCalendar] components:flags fromDate:utc toDate:currentDate options:0];
NSLog(@"difference::%d",[difference day]);
NSLog(@"difference::%d",[difference hour]);
NSLog(@"difference::%d",[difference minute]);
EDIT: Outputs
Post date::2013-03-18 03:20:09
local 2013-03-18 03:20:09
difference::0
difference::13
difference::2
Upvotes: 0
Views: 3100
Reputation: 5268
NSString *openingHour = @"15:05:35 ";
NSDateFormatter *workingHoursFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[workingHoursFormatter setTimeZone:timeZone];
[workingHoursFormatter setDateFormat:@"HH:mm"];
NSDate *openingTime = [workingHoursFormatter dateFromString:openingHour];
Can you try the above code it works for me , alter the date format to your need
Upvotes: 0
Reputation: 4614
Replace your code from...
fmt.timeZone = [NSTimeZone systemTimeZone];
as like this
fmt.timeZone = [NSTimeZone localTimeZone];
This will works for you...
Upvotes: 1