使用NSDateComponents获得一周的第一天:改变一年

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用NSDateComponents获得一周的第一天:改变一年相关的知识,希望对你有一定的参考价值。

您好我设置firstWeekDay有问题,这是我做的:

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; 
comp = [gregorian components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit) fromDate:targetDate];
[comp setWeekday:2];

NSDate *firstWeekDay = [gregorian dateFromComponents:comp];

如果2011-01-01周六是targetDate,我日历中的第一周周日似乎是2011-12-26周一,但实际上应该是2010-12-26周一。我怎么能做对的?

答案

要解决此问题,请在NSDateComponents中添加NSYearForWeekOfYearCalendarUnit。

例如:

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:curDate];

[comps setWeekday:2]; // 2: monday
firstDayOfTheWeek = [calendar dateFromComponents:comps];
[comps setWeekday:7]; // 7: saturday
lastDayOfTheWeek = [calendar dateFromComponents:comps];
另一答案

试试这个

// adjust them for first day of previous week (Monday)
[comp setWeekday:2];
[comp setWeek:([comp week] - 1)];

请注意,此解决方案假设一周的第一天是星期一。

另一答案

下面还介绍了边缘情况,

- (NSDate *)getFirstDayOfTheWeekFromDate:(NSDate *)givenDate
{
    NSCalendar *calendar = [NSCalendar currentCalendar];


   NSDateComponents *components = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:givenDate];
    [components setWeekday:2]; // 1 == Sunday, 7 == Saturday
    if([[calendar dateFromComponents:components] compare: curDate] == NSOrderedDescending) // if start is later in time than end
    {
        [components setWeek:[components week]-1];
    }

    return [calendar dateFromComponents:components];
}
另一答案

Swift解决方案(感谢@YvesJusot):

let calendar = NSCalendar.currentCalendar()
let comps = calendar.components(NSCalendarUnit.WeekOfYearCalendarUnit|NSCalendarUnit.YearCalendarUnit|NSCalendarUnit.MonthCalendarUnit|NSCalendarUnit.WeekCalendarUnit|NSCalendarUnit.WeekdayCalendarUnit, fromDate: NSDate()) as NSDateComponents

comps.setValue(2, forComponent: NSCalendarUnit.WeekdayCalendarUnit)
let monday = calendar.dateFromComponents(comps)!

println("(monday)")
另一答案

您假设使用动态值而不是硬编码2作为星期一!

NSCalendar* calendar = [NSCalendar currentCalendar];
NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit|NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:[NSDate date]];

comps.weekday = calendar.firstWeekday;
firstDayOfTheWeek = [calendar dateFromComponents:comps];
comps.weekday = calendar.firstWeekday - 1;
lastDayOfTheWeek = [calendar dateFromComponents:comps];
另一答案

我在@JasonGarrett的答案中做了一些修改,它对我有用。

let calendar = NSCalendar.currentCalendar()
let flags : NSCalendarUnit = NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear
let comps = calendar.components(flags, fromDate: NSDate()) as NSDateComponents
                comps.setValue(2, forComponent: NSCalendarUnit.CalendarUnitWeekday)
let monday = calendar.dateFromComponents(comps)!
另一答案
// Make these two methods as categories to NSDate
// Use This to get First date of the week (Monday)

- (NSDate *) getFirstDayOfWeek {

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *compOfDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:self];

    NSInteger numOfDaysToAdd = compOfDate.weekday == 1 ? -6 : (2 - compOfDate.weekday);

    NSDateComponents *compToAdd = [[NSDateComponents alloc] init];

    compToAdd.day = numOfDaysToAdd;

    return [calendar dateByAddingComponents:compToAdd toDate:self options:0];
}

// Use This to get last date of the week (Sunday)

- (NSDate *)getLastDayOfWeek {

    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];

    NSDateComponents *compOfDate = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday) fromDate:self];

    NSInteger numOfDaysToAdd = compOfDate.weekday == 1 ? 0 : (8 - compOfDate.weekday);

    NSDateComponents *compToAdd = [[NSDateComponents alloc] init];

    compToAdd.day = numOfDaysToAdd;

    return [calendar dateByAddingComponents:compToAdd toDate:self options:0];
}

以上是关于使用NSDateComponents获得一周的第一天:改变一年的主要内容,如果未能解决你的问题,请参考以下文章

NSDateComponents 给出奇怪的结果

如何在mysql中获取日期的一周的第一天?

如何获取当月第一周的日期

使用存储过程将星期一计算为一周的第一天

Bigquery 自定义一周的第一天(默认为星期一)

为啥在使用 NSCalendarUnitEra 时 ordinalityOfUnit 使用星期一作为一周的第一天?