【问题标题】:NSDate and NSDateformatter wrong outputNSDate 和 NSDateformatter 错误输出
【发布时间】:2014-04-05 17:07:43
【问题描述】:

所以,我正在尝试为日出和日落创建一个 NSDate 对象。我根据NSDatePicker 获取日期,从地图获取坐标,从地图获取 GPS 时区。

我使用此代码获取NSDate 对象:https://github.com/MosheBerman/KosherCocoa-legacy 这个获取坐标:https://github.com/digdog/MapKitDragAndDrop 而这个根据坐标获取时区:https://github.com/Alterplay/APTimeZones

现在我的实际位置在洛杉矶,而我用来测试的日出和日落是在丹麦的家中。

-(NSString *)sunriseDate{
    //Create the GeoLocation based on 'latitude' and 'longitude' (getting the from MapKitDragAndDrop) and 'location.timeZone' (getting that from APTimeZones).
    GeoLocation *position = [[GeoLocation alloc] initWithName:@"position" andLatitude:latitude andLongitude:longitude andTimeZone:location.timeZone];

    AstronomicalCalendar *astronomicalCalender = [[AstronomicalCalendar alloc] initWithLocation:position];

    //daysBetween is the value from NSDatePicker
    astronomicalCalender.workingDate = [NSDate dateWithTimeIntervalSinceNow:kSecondsInADay*[self daysBetween]];

    NSDate *sunriseDate = [astronomicalCalender sunrise];
    NSLog(@"Sunrise time: %@", sunriseDate);
    //This spits out: Sunrise time: 2014-03-05 06:09:53 AM +0000 which is the right time.
    NSDateFormatter *sunriseTime = [[NSDateFormatter alloc] init];
    [sunriseTime setDateFormat:@"HH:mm:ss"];

    NSString *sunriseString = [sunriseTime stringFromDate:sunriseDate];
    NSLog(@"Sunrisestring: %@", sunriseString);
    //This spits out: 10:09:53 PM. 
    return sunriseString;

}

为什么会发生这种情况,谁能给我一个解决方案?

【问题讨论】:

    标签: ios timezone nsdate nsdateformatter


    【解决方案1】:

    您需要正确匹配输入格式。

    您可能只对时间感兴趣,但 NSDateFormatter 并不关心。 NSDate 绝不仅仅是一次。它是一个时间点,因此也包括日期。如果没有日期和时间部分,它将无法工作。

    另外,这可能是 Stack Overflow 上被问到最多的问题之一。任何其他 NSDate 到 NSString(反之亦然)的问题都会回答这个问题。

    你的日期格式应该是……

    @"YYYY-MM-dd hh:mm:ss a EEEE"
    

    我相信。反正就是这样。

    【讨论】:

    • 我找不到任何东西,相信我,我一直在寻找。我无法从这个答案中得到任何东西.. 除了如何显示和/或 iPhone 所在的时区。如果你能指出我自我自己搜索以来你所谈论的任何线程的方向已经很短了..(EEE 代表白天,zzzz 代表时区)
    【解决方案2】:

    这会吐出:晚上 10:09:53。这是您所在时区的正确当地时间,与格林威治时间相差 8 小时 日出时间:2014-03-05 06:09:53 AM +0000。就这样。当一个德国人醒来时,你有一个晚上。

    【讨论】:

    • 没错,但我需要在尊重时区的时间内把它吐出来。
    【解决方案3】:

    正如 Fogmeister 所说,您应该在创建 NSDateFormatter 时包含时区。在Apple's Docs, Data Formatting Guide 抢夺:

    固定格式 要为日期格式化程序指定自定义固定格式,请使用 setDateFormat:。格式字符串使用 Unicode 技术标准 #35 中的格式模式。

    Unicode 官方网站:

    http://www.unicode.org/reports/tr35/tr35-31/tr35-dates.html#Date_Format_Patterns

    【讨论】:

      【解决方案4】:

      你可以试试这个:[sunriseTime setDateFormat:@"yyyy-MM-dd hh:mm:ss a EEEE"];

      而不是[sunriseTime setDateFormat:@"HH:mm:ss"];

      【讨论】:

      • 我试过这个。这只返回时区 ekstra,它不会改变输出以匹配坐标的正确时间,''hh:mm:ss'' 部分打印出完全相同的东西。
      【解决方案5】:

      致任何可能遇到同样事情的人。

      我在 github https://github.com/Alterplay/APTimeZones 上找到了一个库,它帮助我根据坐标确定时区。

      然后我用了

      [sunriseTime setTimeZone:location.timeZone];
      

      这为时区提供了正确的时间。

      希望这对任何人都有帮助!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多