【问题标题】:UIDatePicker, +/- 1 day difference when used as a string in UITextViewUIDatePicker,在 UITextView 中用作字符串时相差 +/- 1 天
【发布时间】:2012-06-22 07:42:13
【问题描述】:

我有 2 个日期选择器,每个都遵循以太(280 天) 选择器更改正确,但mydatepicker.date NSlog 或将其放入UITextfield 日期变为日期 - 1 天或日期 + 1 天

这是我的代码:

第一个选择器:

int daysToAdd = -280;  
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd];
// create a calendar
NSCalendar *addingdays = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate = [addingdays dateByAddingComponents:components toDate:dueDatePickerView.date options:0];
lastPerPickerView.date = newDate;
 NSLog(@"%@",newDate);

第二个:

int daysToAdd2 = 280;  
// set up date components
NSDateComponents *components = [[[NSDateComponents alloc] init] autorelease];
[components setDay:daysToAdd2];
// create a calendar
NSCalendar *addingdays2 = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *newDate2 = [addingdays2 dateByAddingComponents:components toDate:lastPerPickerView.date options:0];
dueDatePickerView.date = newDate2;
NSLog(@"%@",newDate2);

如果第一个拣货员的日期是 12-12-2012 输出是 11-12-2012

在我的代码中,我给了他们

的属性
  lastPerPickerView.datePickerMode = UIDatePickerModeDate;

【问题讨论】:

    标签: iphone xcode nsdateformatter uidatepicker nsdatecomponents


    【解决方案1】:

    尝试将此行添加到您的代码中,

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT+0:00"]];
    

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
    

    【讨论】:

      【解决方案2】:

      您得到的输出在逻辑上是正确的。但如果你想根据你的要求输出我认为你应该将值 280 更改为 279。

      【讨论】:

      • 您没有得到任何日期选择器的正确答案?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-12
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2023-03-15
      • 1970-01-01
      • 2014-03-06
      相关资源
      最近更新 更多