简体   繁体   English

获取一个月的所有 NSDate(以及相邻周的日期)

[英]Get all NSDate of a month (and the dates in adjacent weeks)

Given a date (for example today 4 Feb 2014 ), I would like to get a list of NSDate instances that go from Monday 27 Jan 2014 to Sunday 2 Mar 2014 .给定一个日期(例如今天4 Feb 2014 ),我想获取从Monday 27 Jan 2014Sunday 2 Mar 2014NSDate实例列表。

Basically all the dates in the current month, plus the dates from the last week of the previous month, and the first week of the next month, if they share the same week with some of the dates in the current month.基本上是当月的所有日期,加上上个月最后一周和下个月第一周的日期,如果它们与当月的某些日期共享同一周。

How can I achieve this?我怎样才能做到这一点?

在此处输入图片说明

I can think of a way to obtain this (pseudo-code below), but it's way too long and complicated.我可以想出一种方法来获得这个(下面的伪代码),但它太长太复杂了。 Is there any simpler way, like a method that the SDK provides to short cut?有没有更简单的方法,比如SDK提供的快捷方式?

  1. Extract the month component from [NSDate date] (today)从 [NSDate date](今天)中提取月份组件
  2. Construct the first day of the month构造一个月的第一天
  3. Calculate its weekday计算它的工作日
  4. If it's Wednesday (3rd day of the week) then add today-1, today-2 to the list and so on如果是星期三(一周的第 3 天),则将 today-1、today-2 添加到列表中,依此类推
  5. Repeat from step 2, but with the last day of the month从第 2 步开始重复,但在本月的最后一天

Also to all the elitists out there, just because I'm not posting any code, doesn't mean this is not a coding question.同样对于那里的所有精英,仅仅因为我没有发布任何代码,并不意味着这不是一个编码问题。 The problem is real (construct a calendar grid of a month), and finding the right algorithm/method before coding is much better than playing around and manually do the maths with NSDate and NSCalendar (very error prone, as I will need to take into account all the weird cases).问题是真实的(构建一个月的日历网格),并且在编码之前找到正确的算法/方法比使用 NSDate 和 NSCalendar 手动进行数学运算要好得多(非常容易出错,因为我需要考虑考虑所有奇怪的情况)。 I figure many people have already encountered this same problem and if they could share some pointers, great.我想很多人已经遇到过同样的问题,如果他们能分享一些建议,那就太好了。 If you don't want to answer, no need to reply.如果您不想回答,则无需回复。

I'm going to post very general code.我将发布非常通用的代码。 You are going to need to make it specific to your needs.您将需要使其特定于您的需求。

//Setup the calendar object
NSCalendar *calendar = [NSCalendar currentCalendar];

//Get the current date's month
NSUInteger month = [dateComponents month];

//Create an NSDate for the first and last day of the month
NSDateComponents *comp = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
[comp setDay:1];
NSDate *firstOfMonth = [calendar dateFromComponents:comp];
[comp setMonth:[comp month]+1];
[comp setDay:0];
NSDate *lastOfMonth = [calendar dateFromComponents:comp];

//Now get the first and last week number from there
NSUInteger unitFlags = NSWeekCalendarUnit;
//Create a date component object from today's date.
NSDateComponents *firstDateComponents = [calendar components:unitFlags
                                                   fromDate:firstOfMonth // for current date
                                                    options:0];
NSDateComponents *lastDateComponents = [calendar components:unitFlags
                                                   fromDate:lastOfMonth // for current date
                                                    options:0];


NSUInteger firstWeek = [firstDateComponents week];
NSUInteger lastWeek = [lastDateComponents week];

Now that you have the first and last weeks you can start at the first day of the first week and go through the last day of the last week to set up your calendar.现在您有了第一周和最后一周,您可以从第一周的第一天开始,并通过最后一周的最后一天来设置日历。 Good luck.祝你好运。

Swift 5:斯威夫特 5:

extension Date {
    
    var startOfMonth: Date {
        
        let calendar = Calendar(identifier: .gregorian)
        let components = calendar.dateComponents([.year, .month], from: self)
        
        return  calendar.date(from: components)!
    }

    var endOfMonth: Date {
        var components = DateComponents()
        components.month = 1
        components.second = -1
        return Calendar(identifier: .gregorian).date(byAdding: components, to: startOfMonth)!
    }
}

let date = Date()

let firstWeek = Calendar.current.dateComponents([.weekOfYear], from: date.startOfMonth)
let lastWeek = Calendar.current.dateComponents([.weekOfYear], from: date.endOfMonth)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM