如何根據給定的一天獲得當月的實際日期?例如,我想在2017年6月份的星期六檢索所有日期。我怎樣才能做到這一點?示例代碼將非常感謝,因爲我在這方面掙扎了數日。如何獲得特定工作日的一個月中的所有日子?
1
A
回答
3
A DateComponents
有一個weekday
屬性,表示星期幾。平日(在基金會的公曆中)週日編號爲1,週一爲2,...,週六爲7。
一個DateComponents
也有weekdayOrdinal
屬性,代表「the position of the weekday within the next larger calendar unit, such as the month. For example, 2 is the weekday ordinal unit for the second Friday of the month.」
讓我們初始化DateComponents
一些星期六六月2017年它通常是一個好主意,以指定中午的時間,如果你不關心時間,因爲midnight (the default time of day) can cause problems in some time zones on some days。
var components = DateComponents(era: 1, year: 2017, month: 06, hour: 12, weekday: 7)
然後讓我們製作一個日曆。
var calendar = Calendar.autoupdatingCurrent
現在我們可以遍歷所有可能的週日序數。對於每一個,我們都會要求日曆生成一個日期。然後我們要求日曆將日期轉換回年,月,日組件。
在公曆中,有些月份有5個星期六,但大多數有4個。所以當我們要求第5個星期六時,我們可能會在下個月得到一個日期。當發生這種情況時,我們想壓制那個日期。
for i in 1 ... 5 {
components.weekdayOrdinal = i
let date = calendar.date(from: components)!
let ymd = calendar.dateComponents([.year, .month, .day], from: date)
guard ymd.month == components.month else { break }
print("\(ymd.year!)-\(ymd.month!)-\(ymd.day!)")
}
輸出:
2017-6-3
2017-6-10
2017-6-17
2017-6-24
Objective-C的版本:
NSDateComponents *components = [NSDateComponents new];
components.era = 1;
components.year = 2017;
components.month = 6;
components.hour = 12;
components.weekday = 7;
NSCalendar *calendar = NSCalendar.autoupdatingCurrentCalendar;
for (NSInteger i = 1; i <= 5; ++i) {
components.weekdayOrdinal = i;
NSDate *date = [calendar dateFromComponents:components];
NSDateComponents *ymd = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:date];
if (ymd.month != components.month) { break; }
NSLog(@"%ld-%ld-%ld", (long)ymd.year, (long)ymd.month, (long)ymd.day);
}
0
這是使用calendar
方法稱爲enumerateDates
和使用Date
擴展
//month in MM format, year in yyyy format and dayNumber as Int 1 for sunday, 7 for saturday
func datesWith(dayNumber:Int,month:String,year:String) -> [Date]
{
assert(dayNumber >= 1 && dayNumber <= 7, "Day number is wrong")
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
let date = dateFormatter.date(from: year + "-" + month + "-" + "01")
guard date != nil else {
return []
}
var resultDates : [Date] = []
//check if firstDay of month is desired weekday
if(Calendar.current.component(.weekday, from: date!) == dayNumber)
{
resultDates.append(date!)
}
Calendar.current.enumerateDates(startingAfter: date!, matching: DateComponents(weekday: dayNumber), matchingPolicy: Calendar.MatchingPolicy.nextTimePreservingSmallerComponents) { (currentDate, result, stop) in
if(currentDate! > date!.endOfMonth())
{
stop = true
return
}
resultDates.append(currentDate!)
}
return resultDates
}
你的問題的另一個解決方案
擴展
extension Date {
func startOfMonth() -> Date {
return Calendar.current.date(from: Calendar.current.dateComponents([.year, .month], from: Calendar.current.startOfDay(for: self)))!
}
func endOfMonth() -> Date {
return Calendar.current.date(byAdding: DateComponents(month: 1, day: -1), to: self.startOfMonth())!
}
}
使用它
override func viewDidLoad() {
super.viewDidLoad()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
// Do any additional setup after loading the view, typically from a nib.
let datesArray = self.datesWith(dayNumber: 5, month: "06", year: "2017")
for currDate in datesArray {
debugPrint(dateFormatter.string(from: currDate))
}
}
輸出
"2017-06-01"
"2017-06-08"
"2017-06-15"
"2017-06-22"
"2017-06-29"
希望這會有幫助
相關問題
- 1. 我如何得到所有的工作日和日期在一個月
- 2. php在一個月內獲得所有星期日的日期
- 3. 如何獲得C中給定月份的所有日期#
- 4. 如何獲得給定日期後一個月的30日
- 5. 如何獲得特定一週中特定日期的日期
- 6. 如何獲得當月的所有日子?
- 7. 如何取得日期:距離特定日期一個月?
- 8. 時刻如何獲得上個月(12月)的最後一個工作日
- 9. 如何獲得所有日期在一個月
- 10. 如何獲得序平日一個月
- 11. Mysql:獲取一個月中的所有日子
- 12. 如何在Oracle中獲得當前月份的特定日期?
- 13. 獲取特定日期在一個月
- 14. 如何通過`business_time`獲得一個月的最後一個工作日寶石
- 15. 獲取特定工作日的日期
- 16. 如何在特定日期之前獲得幾個月?
- 17. 從jquery UI日曆中選擇一個月的所有工作日
- 18. 獲取R中某個特定工作日的日期
- 19. 下個月的第一個工作日
- 20. 每月cron特定工作日ubuntu
- 21. 如何從2日期起獲得月數和月數的所有月份?
- 22. 如何轉動一個月的日子
- 23. 如何在MySQL查詢中獲得給定月份的第N個工作日的日期?
- 24. Scala - 獲取特定年份的所有月份和日期
- 25. restfb從特定日期起獲得對所有帖子的所有評論
- 26. Power BI - 一個月內的工作日
- 27. 如何獲得每個月的最短日期爲六個月?
- 28. 如何獲得月,年和日在PostgreSQL的兩個日期
- 29. 日期()獲得3個工作日前?
- 30. XCOPY:有沒有辦法只獲得一個特定的日期?