如何根据给定的一天获得当月的实际日期?例如,我想在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:有没有办法只获得一个特定的日期?