【问题标题】:NSDate bug in xcode [duplicate]xcode中的NSDate错误[重复]
【发布时间】:2014-02-06 08:53:18
【问题描述】:

我遇到了问题。当我放置断点并显示 nsdate 对象的值时,它显示正确,但是当我在 NSLog() 中打印它时,它会显示其他时间。

代码:

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    [f setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSDate *startDate = [f dateFromString:@"2014-01-17 15:30:00"];
    NSLog(@"tim %@",startDate);

xcode中断点的图片

nslog o/p:

2014-01-17 11:27:08.348 APP[596:a0b] tim 2014-01-17 10:00:00 +0000

这是xcode的正常行为还是bug?

编辑:

我正在创建基于LocalNotification 的应用程序,所以我想将其用作全球时间,所以如果我的本地时间不同,它应该在2014-01-17 15:30:00 处触发,仅此而已。

编辑 2

根据答案和评论,请使用时区,那么 NSLocaleNSTimeZone 有什么区别,因为我已将 nslocal 设置为 en_US。?

如果我使用默认值,它不会让我有问题吗??我在所有国家的某个特定时间都有火灾本地通知,所以它会影响呼叫本地通知的时间吗?

【问题讨论】:

  • 时区差异。 IST 是印度标准时间,即 UTC + 5:30。
  • 显示的内容还可以。由于您已显示日期而未提及本地日期,因此它采用了 UTC。您可以在输出中看到它:+0000
  • 如果我使用 UTC 它会同时调用所有国家??
  • 在这些链接中检查我的答案 stackoverflow.com/questions/15240170/… , stackoverflow.com/questions/15086781/… 希望这会有所帮助。
  • 检查更新的问题。

标签: ios iphone objective-c xcode nsdate


【解决方案1】:

试试这个

NSDateFormatter *f = [[NSDateFormatter alloc] init];
[f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[f setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
NSDate *startDate = [f dateFromString:@"2014-01-17 15:30:00"];
NSLog(@"tim %@", [f stringFromDate:startDate]);

【讨论】:

    【解决方案2】:

    您必须将时区设置为UTC。像这样

        NSDateFormatter *f = [[NSDateFormatter alloc] init];
        NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
        [f setTimeZone:timeZone];
        [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        NSDate *startDate = [f dateFromString:@"2014-01-17 15:30:00"];
        NSLog(@"tim %@",startDate);
    

    我认为这段代码也解决了你的本地通知触发时间问题。

    如果您想获取当前位置的日期和时间,则可以使用以下代码。这可能会对您有所帮助。

        NSDate* sourceDate = [NSDate date];
    
        NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
        NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    
        NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
        NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
        NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    
        NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
    
        NSTimeZone *timeZone2 = [NSTimeZone systemTimeZone];
        [f setTimeZone:timeZone2];
    
        NSLog(@"%@", destinationDate);
    

    【讨论】:

    • 如果我使用 UTC,它将同时调用所有国家??
    • NSLocale 只是关于当前使用的区域设置的设置,并不代表您所在的实际国家/地区。
    • 请检查更新的问题。
    • 我没有得到你的更新答案。
    • NSDate* sourceDate = your date; 中传递您的源日期并检查以下代码中的输出。
    【解决方案3】:

    在记录 startDate 之前添加这个:

    NSLocale* local = [NSLocale currentLocale];
    

    并使用以下日志:

    NSLog(@"time: %@", [startDate descriptionWithLocale: local]);
    

    编辑:

    -(NSDate *) getLocalTimeFrom:(NSDate *)UTCTime
    {
       NSTimeZone *tz = [NSTimeZone defaultTimeZone];
       NSInteger seconds = [tz secondsFromGMTForDate: UTCTime];
       return [NSDate dateWithTimeInterval: seconds sinceDate: UTCTime];
    } 
    
    -(NSDate *) toGlobalTimeFrom:(NSDate *)localTime
    {
       NSTimeZone *tz = [NSTimeZone defaultTimeZone];
       NSInteger seconds = -[tz secondsFromGMTForDate: localTime];
       return [NSDate dateWithTimeInterval: seconds sinceDate: localTime];
    }
    

    【讨论】:

    • 如果我使用 UTC,它将同时调用所有国家??
    • 检查更新的问题。
    • 我想建议你不要用一些标识符硬编码本地。让它使用默认的本地和时区。一切都会自己处理。
    • 如果我使用默认设置不会让我有问题??我在所有国家的某个特定时间都有触发本地通知,所以它会影响呼叫本地通知的时间吗?
    • 如果您需要根据时区转换日期,您可以使用我在最新答案中添加的内容。
    猜你喜欢
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 2016-12-29
    • 2012-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-04
    相关资源
    最近更新 更多