【问题标题】:UIDatePicker return wrong NSDateUIDatePicker 返回错误的 NSDate
【发布时间】:2011-12-02 12:48:40
【问题描述】:

我在我的应用程序中使用 UIDatePicker,当我选择日期时:

NSDate *date = picker.date;

picker.date 在我选择的日期前一天返回。

知道为什么会这样吗?

【问题讨论】:

  • 您使用的是 UIImagePicker(如您的问题)还是 UIDatePicker(如您的问题标题)?您是否以任何方式自定义日期选择器?
  • 我的错误,我使用的是 UIDatePicker

标签: iphone objective-c uidatepicker


【解决方案1】:

UIDatePicker 将显示您当地时区的日期和时间。但是,NSDate 没有任何时区概念,因为它存储了自参考日期以来的绝对秒数。当NSLogging 一个日期时,它以GMT 显示日期和时间。我希望如果您计算出与 GMT 的本地时区差异,您会发现它是正确的日期。

尝试使用适当的语言环境创建 NSDateFormatter 或 NSCalendar 并通过它传递日期。

有关此常见主题的进一步阅读,请参阅this site written by another SO contributor

【讨论】:

  • 我选择给用户只选择没有时间的日期
  • 没有时间就没有NSDate。你只是没有显示它。答案还是一样。
  • 这里有同样的问题。通过将 setTimeZone:setLocale: 添加到我的 UIDatePicker 来修复。
【解决方案2】:

试试这个对我有用

        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]; 

//我正在输出我的标签,你可以使用任何你喜欢的东西

        NSString *dateOutput = [[[NSString alloc] initWithFormat:@"%@", destinationDate]autorelease];
        self.dateLabel.text = dateOutput;

【讨论】:

    【解决方案3】:

    记得创建 NSDate,然后用有效的时区和日历输出它!

    NSDate 只代表一个绝对时间点。它没有时区(纽约,巴塞罗那,...)或日历(公历,希伯来语,...)的概念。

    UIDatePicker 默认返回一个带有系统 NSCalendarNSTimeZone 的 NSDate,但是当您稍后尝试打印它时,您不会格式化输出。你可能有不匹配的地方。

    因此,首先您需要正确设置 UIDatePicker,然后使用 NSDateFormatter 转换输出,以便它知道正在使用的日历和时区。

    带有 UIDatePicker 的 init 然后打印结果的示例代码:

    - (void)viewDidLoad
    {
       [super viewDidLoad];
    
       // init the UIDatePicker with your values 
       // by default UIDatePicker inits with today, system calendar and timezone
       // Only for teaching purposes I will init with default values
       NSDate * now = [[NSDate alloc] init];
       [_datePicker setDate: now] 
                   animated: YES];       
    
       _datePicker.timeZone = [NSTimeZone localTimeZone];
       _datePicker.calendar = [NSCalendar currentCalendar];
    
       [_datePicker addTarget: self 
                       action: @selector(getDatePickerSelection:) 
             forControlEvents:UIControlEventValueChanged];
    
    }   
    
    
    -(void)getDatePickerSelection:(id) sender
    {
    
       // NSDateFormatter automatically inits with system calendar and timezone
       NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
       // Setup an output style
       [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
       [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    
       // Medium style date, short style time => "Nov 23, 1937 3:30pm"
       NSString *dateString = [dateFormatter stringFromDate:date];
    
    }
    

    检查我对另一个非常相似的问题的回答:

    NSDate output using NSDateFormatter

    【讨论】:

      【解决方案4】:

      你检查时区了吗?

      当您打印 NSDate 时,它将使用 GMT 作为时区。

      如果您将系统时区设置为 NSDateFormatter,您可能会得到另一个日期,因为它会占用时区并相应地计算时间。

      添加这段代码,看看输出是否正确:

      NSDate *date = picker.date;
      NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
      [dateFormatter setDateStyle:NSDateFormatterMediumStyle];
      [dateFormatter setTimeZone:NSDateFormatterNoStyle];
      [dateFormatter setLocale:[NSLocale currentLocale]];
      NSLog(@"Date: %@", [dateFormmater stringFromDate:date]);
      [dateFormatter release], dateFormatter = nil;
      

      【讨论】:

      • 那么我怎样才能只获得用户选择的日期
      • 您有用户选择的日期,但没有时区。当前系统使用的时区。
      【解决方案5】:

      只需添加一行代码

      self.datePicker.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
      

      0 代表 GMT 00 。根据您的时区添加。

      【讨论】:

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