zoukankan      html  css  js  c++  java
  • 通过今天的日期,获得本周,本月,上月,本年的开始结束日期

    //获取本周 本月 本年的 开始结束日期
    - (void)getMonthBeginAndEndWith:(NSDate *)newDate{
        if (newDate == nil) {
            newDate = [NSDate date];
        }
        double interval = 0;
        NSDate *beginDate = nil;
        NSDate *endDate = nil;
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        [calendar setFirstWeekday:2];//设定周一为周首日
        BOOL ok = [calendar rangeOfUnit:NSMonthCalendarUnit startDate:&beginDate interval:&interval forDate:newDate];
    //分别修改为 NSDayCalendarUnit NSWeekCalendarUnit NSYearCalendarUnit 
        if (ok) {
            endDate = [beginDate dateByAddingTimeInterval:interval-1];
        }else {
            return;
        }
        NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];
        [myDateFormatter setDateFormat:@"yyyy.MM.dd"];
        NSString *beginString = [myDateFormatter stringFromDate:beginDate];
        NSString *endString = [myDateFormatter stringFromDate:endDate];
           

    Nsstring * str = [NSString stringWithFormat:@"%@-%@",beginString,endString];

    Nslog(@"%@",str);// 年 月 周  的开始结束日期;

    }
     
     

    // 获得当前日期的上个月的日期

    - (void)getBeformouthData

    {

        NSInteger year,month,day;

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

        NSDate *now = [NSDate date];;

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

        NSInteger unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit |

        NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;

        

        comps = [calendar components:unitFlags fromDate:now];

        year = [comps year];

        month = [comps month];

        day = [comps day];

        

        

        NSLog(@"现在是:%d%d%d ",year,month,day);

        //   month获取现在的月份,然后 判断是上个月份是否为12

        if (month!=1) {

            //       如果mouth!=1,那么上个月月初为1号,年份不变

            NSLog(@"上个月月初:%d-%d-1 ",year,month-1);

            //        处理上个月份为2月的特殊情况,

            if (month!=3) {

                //            如果上个月的月份为 4  6  9  11 月份,他们月末是30

                if (month == 5 || month==7 || month ==10 || month ==12) {

                    NSLog(@"上个月月末:%d-%d-30 ",year,month-1);

                }

                else {

                    NSLog(@"上个月月末:%d-%d-31 ",year,month-1);

                }

            }

            //        如果本月是3月份,上个月为为2月份,闰年2月月末29天,非闰年28

            else {

                if ((year%4==0 && year%100!=0) || year%400==0) {

                    NSLog(@"上个月月末:%d-%d-29 ",year,month-1);

                }

                else {

                    NSLog(@"上个月月末:%d-%d-28 ",year,month-1);

                }

            }

            

        }

        //    如果当前月份是1月,上个月份就是12  year-1,month=12

        else {

            NSLog(@"上个月月初为 %d-12-1",year-1);

            NSLog(@"上个月月末为 %d-12-31",year-1);

        }

    }

     

    //获取当前日期的上周一和上周日的时间

    -(NSString *)getWeekTime

    {

        NSDate *nowDate = [NSDate date];

        NSCalendar *calendar = [NSCalendar currentCalendar];

        NSDateComponents *comp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit fromDate:nowDate];

        

        //获取今天周几

        NSInteger weekDay = [comp weekday]-1;

        

        //获取今天是几号

        NSInteger day = [comp day];

        NSLog(@"今天周 %d ==========今天%d========",weekDay,day);

        

        

        //计算当前日期和本周的周一和周日相差天数

        long firstOffset,lastOffset;

        if(weekDay == 1)

        {

            firstOffset = -6;

            lastOffset = 0;

        }else{

            firstOffset = [calendar firstWeekday] - weekDay-7 ;

            lastOffset =  -weekDay;

        }

        

        NSLog(@"firstOffset %ld=======last %ld",firstOffset,lastOffset);

        

        //在当前日期基础上加上差的天数

        NSDateComponents *firstDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit  fromDate:nowDate];

        [firstDayComp setDay:day + firstOffset];

        NSDate *firstDayOfWeek = [calendar dateFromComponents:firstDayComp];

        

        NSDateComponents *lastDayComp = [calendar components:NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit   fromDate:nowDate];

        [lastDayComp setDay:day + lastOffset];

        NSDate *lastDayOfWeek = [calendar dateFromComponents:lastDayComp];

        

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

        [formatter setDateFormat:@"yyyy-MM-dd"];

        NSString *firstDay = [formatter stringFromDate:firstDayOfWeek];

        NSString *lastDay = [formatter stringFromDate:lastDayOfWeek];

        NSLog(@"%@=======%@",firstDay,lastDay);

        

        //上周的周一 和周日日期

        NSString *dateStr = [NSString stringWithFormat:@"%@%@",firstDay,lastDay];

        

        NSLog(@"date=======%@",dateStr);

        

        /**

         *  firstDay 本周周一

            lastDay 本周周末

         */

        

        return dateStr;

    }

        

        NSTimeInterval secondsPerDay = 24 * 60 * 60; 

        //昨天时间

        NSDate *yesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay];

        NSString * yestodayStr = [[NSString stringWithFormat:@"%@",yesterday] substringToIndex:10];

        //前天时间

        NSDate *beforeyesterday = [[NSDate alloc] initWithTimeIntervalSinceNow:-secondsPerDay*2];

        NSString * beforeyesterdayStr = [[NSString stringWithFormat:@"%@",beforeyesterday] substringToIndex:10];

  • 相关阅读:
    114自定义UITableViewCell(扩展知识:为UITableViewCell添加动画效果)
    101在检索框中添加一个书签按钮(扩展知识:在检索框中添加一个范围条)
    088实现自动倒计时功能
    086设置日期选择器框的显示样式
    Shell if else
    Shell数组
    Shell字符串
    Shell运算符
    数据挖掘标准流程规范
    Shell转义字符与变量替换
  • 原文地址:https://www.cnblogs.com/weipeng168/p/5320722.html
Copyright © 2011-2022 走看看