IOS 4.1 NSDateComponents 给出的日期与更高版本的 IOS 不同

发布于 12-01 21:53 字数 1551 浏览 2 评论 0原文

我创建了一个 NSDate 类别,它会给我一个“上周日”日期。

+(NSDate *)sunday{
  // I need 2 dates yesterday and today
  NSCalendar *gregorian   = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
  NSDate *now = [NSDate date];
  NSLog(@"Current date: %@", now);
  NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
  [components setWeekday:1]; //Monday 
  [components setHour:0]; //8a.m.
  [components setMinute:0];
  [components setSecond:0];
  NSLog(@"Sunday: %@", [gregorian dateFromComponents:components]);
  return [gregorian dateFromComponents:components]; 
}

这给了我几个不同的日期:

IOS 4.2

Sunday: 0001-08-28 05:50:36 +0000

IOS 4.1

Sunday: 0001-09-04 05:50:36 GMT

通知 IOS 4.1 给我一个未来的日期,而 IOS 4.2 给我一个以前的日期,这就是我想要的。

我知道显示的年份是 0001,但未使用年份。

我还没有发现其他人有同样的问题,所以也许我做错了什么。但是,我不确定那是什么。有人见过这个吗?

编辑 这是我的工作代码:

+(NSDate *)sunday{
  NSCalendar *gregorian   = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
  NSDate *now = [NSDate date];
  NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
  [components setWeekday:[gregorian firstWeekday]]; //Sunday
  [components setHour:0]; 
  [components setMinute:0];
  [components setSecond:0];
  return [gregorian dateFromComponents:components];
}

谢谢!!

I have created an NSDate category that would give me a "last sunday" date.

+(NSDate *)sunday{
  // I need 2 dates yesterday and today
  NSCalendar *gregorian   = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
  NSDate *now = [NSDate date];
  NSLog(@"Current date: %@", now);
  NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
  [components setWeekday:1]; //Monday 
  [components setHour:0]; //8a.m.
  [components setMinute:0];
  [components setSecond:0];
  NSLog(@"Sunday: %@", [gregorian dateFromComponents:components]);
  return [gregorian dateFromComponents:components]; 
}

This give me a couple different dates:

IOS 4.2

Sunday: 0001-08-28 05:50:36 +0000

IOS 4.1

Sunday: 0001-09-04 05:50:36 GMT

Notice
That IOS 4.1 give me a future date, whereas IOS 4.2 gives me a previous date, which is what I want.

I understand that the year is 0001 is shown, but the year is not used.

I have not yet found where others are having this same issue, so perhaps I'm doing something wrong. But, I'm not sure what that is. Has anyone seen this before?

EDIT
Here is my working code:

+(NSDate *)sunday{
  NSCalendar *gregorian   = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
  NSDate *now = [NSDate date];
  NSDateComponents *components = [gregorian components:(NSWeekCalendarUnit | NSYearCalendarUnit | NSWeekdayCalendarUnit ) fromDate:now];
  [components setWeekday:[gregorian firstWeekday]]; //Sunday
  [components setHour:0]; 
  [components setMinute:0];
  [components setSecond:0];
  return [gregorian dateFromComponents:components];
}

Thanks!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年里的时光2024-12-08 21:53:54

您可能应该重新考虑您关于未使用年份的断言。当然,2011-09-04 和(根据 NSGregorianCalendar 用于 1582-10-15 之前的日期的儒略历)0001-09-04 都是星期日,所以看起来这并不重要。但明年就不会匹配了。

两台设备上出现不同结果的问题很可能是因为您的设备设置为不同的区域,对于哪一天是一周的第一天或哪一周是一年的第一天有不同的规则。您可以检查 NSCalendar 对象上的 firstWeekday 属性来确定第一个,而 minimumDaysInFirstWeek 将帮助确定后者。

You should probably reconsider your assertion that the year is not used. Sure, 2011-09-04 and (by the Julian calendar used by NSGregorianCalendar for dates before 1582-10-15) 0001-09-04 are both Sundays so it seems like it doesn't matter. But next year it won't match up.

Your problem with the differing results on the two devices is most likely that your devices are set to different regions, with different rules about which day is the first day of the week or which week is the first of the year. You can check the firstWeekday property on your NSCalendar object to determine the first, and minimumDaysInFirstWeek will help with the latter.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文