簡體   English   中英

獲取給定月份的開始日期和結束日期的所有周(NSDate 目標 C)

[英]Get All Weeks with start date and end date of given month (NSDate Objective C)

我正在嘗試列出給定月份的所有周,例如開始日期 2020 年 9 月 6 日 結束日期 2020 年 9 月 12 日

如果一周在下個月結束,它還應該顯示該日期,如開始日期 2020 年 9 月 27 日結束日期 2020 年 10 月 3 日

一個 function 將 NSDate 作為輸入並使用 NSArray 或 NSDictionary 返回日期數組(一周的開始和結束日期),任何東西都可以工作,只需要給定 NSDate 的開始和結束日期

我已經嘗試過 DateTools、Datez 和其他 pod,但仍然無法獲得這個

    -(void) getWeeksOfTheMonthCurrent {
    NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *oneDayAgoComponents = [[NSDateComponents alloc] init];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];

    for(int currentdateindexpath=0;currentdateindexpath<=10currentdateindexpath++)
    {

        NSDateComponents* subcomponent = [calendar components:NSCalendarUnitWeekday
                                                     fromDate:[NSDate date]];
        
        [oneDayAgoComponents setDay: 0 - ([subcomponent weekday] - 1)];
        
        NSDate *beginningOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                            toDate:[NSDate date] options:0];
        [formatter setDateFormat:@"dd/MM/yy"];
        NSString *weekstartdate = [formatter stringFromDate:beginningOfWeek];
        NSLog(@"weekstartdate %@",weekstartdate);

        
        NSDateComponents *components =
        [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth |
                              NSCalendarUnitDay) fromDate: beginningOfWeek];
        beginningOfWeek = [calendar dateFromComponents:components];
        
        [oneDayAgoComponents setDay:7- ([subcomponent weekday])];
        
        NSDate *endOfWeek = [calendar dateByAddingComponents:oneDayAgoComponents
                                                      toDate:[NSDate date] options:0];
        [formatter setDateFormat:@"dd/MM/yy"];
        NSString *weekendtdate = [formatter stringFromDate:endOfWeek];
        NSLog(@"weekendtdate %@",weekendtdate);
    }
    
    
}

我已經嘗試使用此代碼獲取開始周日期和結束周日期,但這不是我想要的,我只是想要一個 function,它以 NSDate 作為參數並返回包含開始周日期和結束周日期的對象數組

您不需要使用 3rd 方庫來執行此操作,為此類事情提供了大量的日期函數(主要參見NSCalendar的文檔)。

這個答案在 Swift 中,但我會讓你弄清楚轉換。

let calendar = Calendar.current
let now = Date()

let monthInterval = calendar.dateInterval(of: .month, for: now)!

var weekIntervals: [DateInterval] = []
var startDate = monthInterval.start

while monthInterval.contains(startDate) {
    weekIntervals.append(calendar.dateInterval(of: .weekOfMonth, for: startDate)!)
    startDate = calendar.date(byAdding: .weekOfYear, value: 1, to: startDate)!    
}

// Not sure why this is needed, seems like the above should handle it
let finalWeek = calendar.dateInterval(of: .weekOfMonth, for: startDate)!
if finalWeek.start < monthInterval.end {
    weekIntervals.append(finalWeek)
}

打印結果給出...

let formatter = DateIntervalFormatter()
formatter.dateStyle = .medium
weekIntervals.forEach {
    print(formatter.string(from: $0)!)
}


// Jul 26, 2020, 12:00 AM – Aug 2, 2020, 12:00 AM
// Aug 2, 2020, 12:00 AM – Aug 9, 2020, 12:00 AM
// Aug 9, 2020, 12:00 AM – Aug 16, 2020, 12:00 AM
// Aug 16, 2020, 12:00 AM – Aug 23, 2020, 12:00 AM
// Aug 23, 2020, 12:00 AM – Aug 30, 2020, 12:00 AM
// Aug 30, 2020, 12:00 AM – Sep 6, 2020, 12:00 AM

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM