0

In my app, I'm using NSTimer and using initWithFireDate. I want to fire the timer after one hour from now. "Now" means local time zone. When ever I'm trying to get the add add one hour to it and trying to print it, its printing the time in GMT and not according to my timezone. How can I fix this bug? I want NSDate with 1 hour from now depending on local time zone.

5 Answers 5

2

This code would help you get the local time and you can take it forward from there accordingly:

- (NSDate *)currentLocalTime {
      NSDate *aTriggerDate = [NSDate date];

     NSTimeZone *aSourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
     NSTimeZone *aTriggerTimeZone = [NSTimeZone systemTimeZone];

     NSInteger aSourceGMTOffset = [aSourceTimeZone secondsFromGMTForDate:aTriggerDate];
     NSInteger aTriggerGMTOffset = [aTriggerTimeZone secondsFromGMTForDate:aTriggerDate];
     NSTimeInterval anInterval = aTriggerGMTOffset - aSourceGMTOffset;

     NSDate *aFinalDate = [[NSDate alloc] initWithTimeInterval:anInterval sinceDate:aTriggerDate];

    return aFinalDate;
}
1

NSDateFormatter *formater=[[NSDateFormatter alloc] init]; formater.timeZone=[NSTimeZone localTimeZone]; use this for to localized your time zone.

2
  • You do anything with NSDateFormatter and put it back to NSDate, it won't work.
    – Satyam
    Sep 6, 2011 at 6:42
  • it workable because u write in ur question u want to time according to ur local time zone. if u use this in device. Sep 6, 2011 at 7:22
0

You can do it this with NSDateFormatter taking Different Formats of the Date String. Just try it out , You will ge your expected answer.

1
  • You do anything with NSDateFormatter and put it back to NSDate, it won't work.
    – Satyam
    Aug 29, 2011 at 9:36
0

After getting the time, find the time zone and add the time difference accordingly.

0

This is my version of Bikramjit Singh's code in Swift (only worked for me by using "UTC" as the abbreviation). Hope it helps:

func testTimeZone(){

    let currentLocalTime = NSDate()

    let sourceTimeZone = NSTimeZone(abbreviation: "UTC")
    let triggerTimeZone = NSTimeZone.systemTimeZone()

    let sourceGTMOffset = sourceTimeZone?.secondsFromGMTForDate(currentLocalTime)
    let triggerGTMOffset = triggerTimeZone.secondsFromGMTForDate(currentLocalTime)

    let interval = triggerGTMOffset - sourceGTMOffset!

    let finalDate = NSDate(timeInterval: NSTimeInterval.init(interval), sinceDate: currentLocalTime)

    Swift.print("Current local time: \(currentLocalTime)")
    Swift.print("Source Time Zone (GTM): \(sourceTimeZone)")
    Swift.print("Trigger Time Zone (System Time Zone): \(triggerTimeZone)")
    Swift.print("Source GTM Offset: \(sourceGTMOffset)")
    Swift.print("Trigger GTM Offset: \(triggerGTMOffset)")
    Swift.print("Interval (Source - Trigger Offsets): \(interval)")
    Swift.print("Final Date (Current + interval): \(finalDate)")


}

UPDATE

I actually found a shorter way to get the date, this way has worked better for me:

    var currentDate: NSDate {
    let currentLocalTime = NSDate()

    let localTimeZone = NSTimeZone.systemTimeZone()
    let secondsFromGTM = NSTimeInterval.init(localTimeZone.secondsFromGMT)
    let resultDate = NSDate(timeInterval: secondsFromGTM, sinceDate: currentLocalTime)

    return resultDate
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.