-2

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.

Community
  • 1
  • 1
Flipper
  • 1,107
  • 1
  • 11
  • 32
  • I don't follow. What "trouble" are you having? Where does that date/time come from in the first place? – Droppy Jun 24 '15 at 13:05
  • Usually you don't need to do nothing with time zone, NSDate is always at 0000 and everything should work fine. I your case I don't sse any need of messing with time zones – Andrea Jun 24 '15 at 13:06
  • This UTC at the end is causing me trouble and I want to show local notification after 5 seconds. I need to have time interval in my fireDate – Flipper Jun 24 '15 at 13:08
  • @Andrea Ok, I have tried as you said it, it goes to didReceiveLocalNotification. But now it is just not showing as banner. – Flipper Jun 24 '15 at 13:11
  • So you're saying you are having trouble creating the `NSDate` necessary to define when a local notification should be fired? In which case `NSDate *date = [NSDate dateWithTimeIntervalSinceNow:numSeconds]` will work. – Droppy Jun 24 '15 at 13:23
  • @Flipper I don't think that it has to deal with date timezone issue. It seems a different problem. – Andrea Jun 24 '15 at 13:42
  • @Andrea Yes you are right. I've managed as you said. Thank you – Flipper Jun 24 '15 at 14:39

1 Answers1

0

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);
Code Hunterr
  • 175
  • 9