获取本周的周数以及周一和周日的日期(iOS 上的 Objective-C)

发布于 12-19 12:37 字数 28 浏览 4 评论 0原文

如何获得当前周数以及本周周一和周日的日期?

how is it possible to get the current weeknumber and the date of monday and sunday in the current week?

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

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

发布评论

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

评论(1

最近可好2024-12-26 12:37:25

我不确定这有多有效,因为它主要是从其他答案中拼凑而成的

并快速浏览一下苹果文档的

示例:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7

NSDateComponents *components = [gregorian components:NSWeekOfYearCalendarUnit fromDate:today];

NSUInteger weekOfYear = [components weekOfYear];

NSDate *mondaysDate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysDate interval:NULL forDate:today];

NSLog(@"%ld %@", weekOfYear, mondaysDate);

I'm not sure how effeicient this is as it's mainly thrown together from, other answers

and a quick skim of the apple docs for

Example:

NSDate *today = [NSDate date];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
gregorian.firstWeekday = 2; // Sunday = 1, Saturday = 7

NSDateComponents *components = [gregorian components:NSWeekOfYearCalendarUnit fromDate:today];

NSUInteger weekOfYear = [components weekOfYear];

NSDate *mondaysDate = nil;
[gregorian rangeOfUnit:NSWeekCalendarUnit startDate:&mondaysDate interval:NULL forDate:today];

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