iOS Convert UTC date of NSDate to Localtime fo local notifications

628 Views Asked by At

I'm having trouble converting NSDate date to local DateTime. I'm using Local Notifications and my fireDate property is ok except UTC component in my date.

This is NSDate date and converted date to my local timezone

enter image description here

This is my conversion code as found here Convert UTC NSDate to local Timezone Objective-C

NSDate *someDateInUTC = [NSDate date];
NSTimeInterval timeZoneSeconds = [[NSTimeZone localTimeZone] secondsFromGMT];
NSDate *dateInLocalTimezone = [someDateInUTC dateByAddingTimeInterval:timeZoneSeconds];

I'm adding five seconds to my time component so this is my fireDate:

enter image description here

I'm using push notifications and when the user is somewhere in the app, I want to present him local notification from my push notification. This code is in my - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo method. I've implemented method for local notifications:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
  UIApplicationState applicationState = application.applicationState;
  if (applicationState == UIApplicationStateBackground) {
      [application presentLocalNotificationNow:notification];
  }
}

I'm thinking that this UTC component in my dateInLocalTimezone is causing my trouble but I don't know how to solve it.

1

There are 1 best solutions below

0
On

Ok so i read your question. and i have then below example for you.

here i am converting Local time to UTC Time.

    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    fmt.dateFormat = @"LLL d, yyyy - HH:mm:ss zzz";
    NSDate *utc = [fmt dateFromString:@"June 14, 2012 - 01:00:00 UTC"];
    fmt.timeZone = [NSTimeZone systemTimeZone];
    NSString *local = [fmt stringFromDate:utc];
    NSLog(@"%@", local);