美文网首页
年月周日

年月周日

作者: wpf_register | 来源:发表于2018-09-09 18:29 被阅读10次

NSCalendar 功能很强大,多次调用时需要考虑性能问题,但必要的使用会在日期计算时事半功倍。

计算周次所在日期范围
-(NSString*)timeConversionYear:(NSInteger)year WeakOfYear:(NSInteger)weekofYear
{
    //周次的范围日期  几月几日 - 几月几日
    NSString *weekDate = @"";
    //时间轴 取每一年的六月一号 没有特别的含义
    NSString *timeAxis = [NSString stringWithFormat:@"%ld-06-01 12:00:00",(long)year];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //获得了时间轴
    NSDate *date = [dateFormatter dateFromString:timeAxis];
    
    //日历类 提供大部分的时间计算接口
    NSCalendar *calendar = [NSCalendar currentCalendar];
    /**这两个参数的设置影响着周次的个数和划分*****************/
    [calendar setFirstWeekday:2]; //设置每周的开始是星期一
    [calendar setMinimumDaysInFirstWeek:7]; //设置一周至少需要几天
    /****************/
    //一个封装了具体年月日、时秒分、周、季度等的类
    NSDateComponents *comps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                          fromDate:date];
    
    //时间轴是当前年的第几周
    NSInteger todayIsWeek = [comps weekOfYear];
    //第几周的字符串格式
    //NSString *todayIsWeekStr = [NSString stringWithFormat:@"%ld",(long)todayIsWeek];
    
    //获取时间轴是星期几 1(星期天) 2(星期一) 3(星期二) 4(星期三) 5(星期四) 6(星期五) 7(星期六)
    NSInteger todayIsWeekDay = [comps weekday];
    
    //得到时间轴是几号
    // NSInteger todayIsDay = [comps day];
    // 计算当前日期和这周的星期一和星期天差的天数
    //firstDiff 星期一相差天数 、 lastDiff 星期天相差天数
    long firstDiff,lastDiff;
    if (todayIsWeekDay == 1) {
        firstDiff = -6;
        lastDiff = 0;
    }else
    {
        firstDiff = [calendar firstWeekday] - todayIsWeekDay;
        lastDiff = 8 - todayIsWeekDay;
    }
    
    NSDate *firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*firstDiff sinceDate:date];
    NSDate *lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*lastDiff sinceDate:date];
    
    long weekdifference = weekofYear - todayIsWeek;
    
    firstDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:firstDayOfWeek];
    lastDayOfWeek= [NSDate dateWithTimeInterval:24*60*60*7*weekdifference sinceDate:lastDayOfWeek];
    // NSLog(@"星期一的日期 %@",[dateFormatter stringFromDate:firstDayOfWeek]);
    // NSLog(@"星期天的日期 %@",[dateFormatter stringFromDate:lastDayOfWeek]);
    
    NSDateComponents *firstDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                        fromDate:firstDayOfWeek];
    NSDateComponents *lastDayOfWeekcomps = [calendar components:(NSCalendarUnitWeekOfYear | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal | NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay)
                                                       fromDate:lastDayOfWeek];
    
    NSInteger startMonth = [firstDayOfWeekcomps month];
    NSInteger startDay = [firstDayOfWeekcomps day];
    
    NSInteger endmonth = [lastDayOfWeekcomps month];
    NSInteger endday = [lastDayOfWeekcomps day];
    weekDate = [NSString stringWithFormat:@"%ld.%ld.%ld-%ld.%ld.%ld",year,(long)startMonth,(long)startDay,year,(long)endmonth,(long)endday];
    return weekDate;
}
计算年有多少周
+(NSInteger)weeksInOneYear:(NSInteger)year
{
    NSString *timeAxis = [NSString stringWithFormat:@"%ld-12-31 12:00:00",(long)year];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    //获得了时间轴
    NSDate *date = [dateFormatter dateFromString:timeAxis];
    //日历类 提供大部分的时间计算接口
    NSCalendar *calendar = [NSCalendar currentCalendar];
    [calendar setFirstWeekday:2];
    [calendar setMinimumDaysInFirstWeek:7];
    
    //    NSRange range = [calendar rangeOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
    NSInteger length = [calendar ordinalityOfUnit:NSCalendarUnitWeekOfYear inUnit:NSCalendarUnitYear forDate:date];
    return length;
}

相关文章

  • 年月周日

    NSCalendar 功能很强大,多次调用时需要考虑性能问题,但必要的使用会在日期计算时事半功倍。 计算周次所在日...

  • 做自己的CEO,年月周日体系

    又到年底制定来年年度目标的时候了。做自己的CEO课程已经来到了第五周,从上到下的课程思路让我在前面四周倍感辛苦,我...

  • 年月周日目标管理体系落地

    四个收获 1. 目标设定的3个工具 概念工具:成果、未来状态和目的 目标就是个人所期望的成果,是希望达到的未来状态...

  • 2020年月日践行打卡19/90

    2020年月日践行打卡19/90 【更新打卡】 打卡时间:2020年3月29日 周日 小雨 4/14度 90天连续...

  • 年月

    村中有两棵树,一棵是白桦树,另一棵不是白桦树,此时已无法凭记忆归其所属,只记得其样貌类似于槐树,但当前已无法确认,...

  • 年月

    两人四目相对几秒后,男人转身离去,女人站在原地。隔夜酒还是有些醉人,她答应自己这是最后一次听他的话。 她还忘不...

  • 年月

    昨天,去填了一张表,一张干部登记表,好玩得很,还干部呢?干什么的部?(王小波)好玩得很。哈哈,好玩得很! 一路上只...

  • 年月

    年年月月, 似月月年年; 人来人往, 非人往人来。

  • 年月

    斜阳渐散 缱绻一日 皱纹张显了寸光 一条条 聚欢.集乐. 还来 日月流趟 荏苒一年 鬓发书写了韶华 一根根 叙忧....

  • 年月

    一月你从远方来 顺手捎来了二月 三月带着满满的春意也悄悄的跟来 四月蹑手蹑脚的拉着三月的小衣角 嘘,不要让五月知道...

网友评论

      本文标题:年月周日

      本文链接:https://www.haomeiwen.com/subject/gfedgftx.html