【问题标题】:Get the date of sunday of the current week in IOs在 IO 中获取当前星期的星期日日期
【发布时间】:2015-07-08 10:20:33
【问题描述】:

我已搜索找到特定星期的星期日日期的解决方案。我找到了已回答但未显示正确日期的问题。

NSCalendar *calendar = [NSCalendar currentCalendar];
unsigned unitFlags = NSCalendarUnitYear | NSCalendarUnitMonth |  NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitWeekday | NSCalendarUnitWeekOfYear;
NSDateComponents *comps = [calendar components:unitFlags fromDate:[NSDate date]];
comps.weekday = 1;  //Sunday
comps.weekOfMonth = 1;
NSDate *sundayDate = [calendar dateFromComponents:comps];

当我打印出它给出的日期时。

2015-07-08 10:14:00 +0000

它是当前日期。我做错了什么?

【问题讨论】:

    标签: ios nsdate nscalendar nsdatecomponents


    【解决方案1】:

    希望对你有帮助:

        NSDate *today = [NSDate date];
        NSCalendar *calenderObj = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        calenderObj.firstWeekday = 1; // Sunday = 1, 
         // NSDateComponents *components = [calenderObj components:NSWeekOfYearCalendarUnit fromDate:today];
        NSDate *objDate = nil;
     [calenderObj rangeOfUnit:NSWeekCalendarUnit startDate:&objDate interval:NULL forDate:today];
        NSLog(@" === %@",  objDate);
    

    输出:

    === 2015-07-15 10:26:00 +0000

    【讨论】:

    • 你没有使用 components 对象做任何事情。
    • @mattsson 是的,不使用组件,因为上述问题所需的控制台已经在 calenderObj ...
    猜你喜欢
    • 2017-11-07
    • 1970-01-01
    • 2021-10-23
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多