Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSDateFormatter返回2001年1月1日作为日期_Ios_Nsdateformatter - Fatal编程技术网

Ios NSDateFormatter返回2001年1月1日作为日期

Ios NSDateFormatter返回2001年1月1日作为日期,ios,nsdateformatter,Ios,Nsdateformatter,我正在服务器上以GMT格式存储值。 我将其下载到设备上,并通过以下功能将其转换为用户本地时间: - (void)convertTimeZone { //Converts the match timing from GMT to the users local time NSString *serverDateString = [NSString stringWithFormat:@"%@-%@-%@ %@:%@ GMT", year, month, day, hour, min]

我正在服务器上以GMT格式存储值。 我将其下载到设备上,并通过以下功能将其转换为用户本地时间:

- (void)convertTimeZone
{
    //Converts the match timing from GMT to the users local time
    NSString *serverDateString = [NSString stringWithFormat:@"%@-%@-%@ %@:%@ GMT", year, month, day, hour, min];

    NSDateFormatter *dateFormatter = [NSDateFormatter new];
    dateFormatter.dateFormat       = @"yyyy-MM-dd HH:mm zzz";

    NSDate *serverDate             = [dateFormatter dateFromString:serverDateString];

    NSString *localDateString      = [dateFormatter stringFromDate:serverDate];
    NSDate *localDate              = [dateFormatter dateFromString:localDateString];


    NSDateComponents *components = [[NSCalendar currentCalendar] components:NSHourCalendarUnit |NSMinuteCalendarUnit |NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit fromDate:localDate];

    min   = [NSString stringWithFormat:@"%d",[components minute]];
    hour  = [NSString stringWithFormat:@"%d",[components hour]];
    day   = [NSString stringWithFormat:@"%d",[components day]];
    month = [NSString stringWithFormat:@"%d",[components month]];
    year  = [NSString stringWithFormat:@"%d",[components year]];

}
几乎每个人在转换后都有适当的时间。然而,很少有用户写信说他们收到的日期是2001年1月1日,这是iOS和OSX上的初始化日期。 日期是格林尼治标准时间和当地日期之间的时差

服务器正确设置并接收年、月、日、小时和分钟的值。我知道,因为其他一些值显示正确

然而,这种日期-时间转换过程在少数情况下出错

为什么会这样

因为它从未在我的设备上发生过,但也发生在其他国家的用户身上,所以我不知道如何重现它。他们试图重新启动,但仍然得到了错误的日期。
谢谢。

您未能按照要求设置区域设置。因此,可以根据用户的设置修改您设置的日期格式


显式设置区域设置以解决问题。

每当您使用
NSDateFormatter
读取或发出需要采用特定格式的日期时,您都需要执行以下操作:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"]];

这将防止出现所示的问题,例如..

如果看不到您的输入和输出,就不可能找到您可能出错的所有位置。(顺便说一句,-1表示在出现“iOS bug”的可能性非常小的情况下声明该bug。)您的代码完全没有意义——您将字符串转换为NSDate,将其转换回字符串,然后再次转换为NSDate,所有这些都使用相同的日期格式化程序,并且不更改日期格式化程序设置。你希望改变什么?(你希望上面的内容如何从格林尼治时间转换到当地时间?@HotLicks我已经检查了一些东西来隔离问题。将JSON值获取到设备并将其存储起来工作正常。我知道这一点,因为很少有用户只会遇到时间问题,而不会遇到其他文本数据的问题。@HotLicks上述代码会将日期时间从GMT转换为用户的本地时间。100人中有1人发现它不起作用。我还以为是虫子呢。希望bdesham的答案会起作用。那么,它会在任何地方起作用吗?我的应用程序不仅仅是以美国为中心的,这样所有人都能使用吗?谢谢。是的,这将在任何地方都有效,并且手动设置区域设置不会干扰应用程序的本地化。区域设置与时区分开;手动设置它是必要的,以确保您的用户设备的行为符合您的预期,无论其语言或区域设置如何。