【问题标题】:why does this code display the date being SAT where with NSDateComponent setWeekday I used 1 for SUN?为什么此代码显示日期为 SAT,其中 NSDateComponent setWeekday 我使用 1 表示 SUN?
【发布时间】:2011-07-29 13:47:54
【问题描述】:

为什么这段代码显示日期为 SAT,而 NSDateComponent 和 setWeekday 我使用 1 表示 SUN?

// Set Day
NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setWeekday:1];
[comps setHour:13];
[comps setMinute:30];

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDate *tempDate = [gregorian dateFromComponents:comps];

NSDateFormatter *format = [[[NSDateFormatter alloc] init] autorelease];
[format setDateFormat:@"EEE HH:mm"];    
NSString *s = [format stringFromDate:tempDate];
NSLog(@"Finishing : %@", s);

// Gave => 2011-04-05 09:50:28.467 test1[39840:207] Finishing : Sat 13:30

【问题讨论】:

    标签: iphone objective-c ios nsdatecomponent


    【解决方案1】:

    转换仅设置工作日、小时和分钟的 NSDateComponents 显然会为您提供第 1 年的日期。由于这早于儒略历/公历转换,您最终会在某些地方得到奇怪的结果方法用预测的公历解释日期,而其他方法用儒略历解释。

    当我尝试使用更详细的日期格式的代码时,我得到Sat 0001-01-01 13:30:00 -045602。在组件上使用setYear: 后尝试一下,您会得到合理的结果。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      • 2020-12-11
      相关资源
      最近更新 更多