本周的天数列表(List of Days of this week)

编程入门 行业动态 更新时间:2024-10-27 12:27:43
本周的天数列表(List of Days of this week)

我需要一个星期几的列表(周一从周一开始)列出时间卡列表。 我不知道如何实现这一目标。 我已经尝试了一些dateByAddingUnit并从那里减去但它没有产生我想要的结果。

知道如何获取日期列表吗? (即周一将于2016年3月28日星期二,2016年3月29日星期二等等)

I need a list of days of the week (with the week starting on Monday) for a timecard list. Im not sure how to achieve this. I have tried some dateByAddingUnit and subtracting from there but it hasn't produced the results I want.

Any idea how to get a list of the dates? (i.e. Monday would be 3/28/16, Tuesday 3/29/16 and so on)

最满意答案

编辑 :错过了你想要一个星期总是在星期一开始

let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let today = gregorian.startOfDayForDate(NSDate()) let startOfWeek: NSDate if gregorian.component(.Weekday, fromDate: today) == 2 { // Today is Monday startOfWeek = today } else { // Find the last Monday prior to today startOfWeek = gregorian.nextDateAfterDate(today, matchingUnit: .Weekday, value: 2, options: [.SearchBackwards, .MatchPreviousTimePreservingSmallerUnits])! }

Edit: missed that you want a week to always start on a Monday

let gregorian = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)! let today = gregorian.startOfDayForDate(NSDate()) let startOfWeek: NSDate if gregorian.component(.Weekday, fromDate: today) == 2 { // Today is Monday startOfWeek = today } else { // Find the last Monday prior to today startOfWeek = gregorian.nextDateAfterDate(today, matchingUnit: .Weekday, value: 2, options: [.SearchBackwards, .MatchPreviousTimePreservingSmallerUnits])! }

更多推荐

本文发布于:2023-08-05 08:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1429717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:天数   本周   列表   week   Days

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!