iPhone - NSTimer - initWithFireDate 并传递本地时间

发布于 2024-12-01 15:40:52 字数 166 浏览 3 评论 0原文

在我的应用程序中,我使用 NSTimer 并使用 initWithFireDate。 我想从现在开始一小时后启动计时器。 “现在”是指当地时区。 每当我尝试添加一小时并尝试打印它时,它都会打印 GMT 时间,而不是根据我的时区。 我该如何修复这个错误? 我想要从现在起 1 小时后的 NSDate,具体取决于当地时区。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

浮萍、无处依 2024-12-08 15:40:52

此代码将帮助您获取当地时间,您可以相应地从那里向前推进:

- (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;
}

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;
}
铃予 2024-12-08 15:40:52

NSDateFormatter *formater=[[NSDateFormatter alloc] init];
formater.timeZone=[NSTimeZone localTimeZone];
使用它来本地化您的时区。

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

黒涩兲箜 2024-12-08 15:40:52

您可以使用 NSDateFormatter 采取不同格式的日期字符串来做到这一点。只要尝试一下,您就会得到您期望的答案。

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

不寐倦长更 2024-12-08 15:40:52

获取时间后,找到时区并相应添加时差。

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

锦爱 2024-12-08 15:40:52

这是我用 Swift 编写的 Bikramjit Singh 代码的版本(仅适用于我使用“UTC”作为缩写)。希望它有帮助:

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)")


}

更新

我实际上找到了一种更短的获取日期的方法,这种方法对我来说效果更好:

    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
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文