NSDate

作者: PZcoder | 来源:发表于2017-01-16 16:38 被阅读7次

    目录:
    1、时区转换
    2、日期格式转换
    3、时间格式转换
    4、计算今天开始7天内日期、星期(今天,01-13|星期六,01-14|...共7天,使用方法5、6、7、8)
    5、日期转换成字符串
    6、字符串转换成日期
    7、获取当月的天数
    8、获取指定的日期是星期几
    9、获取指定日期的年,月,日,星期,时,分,秒信息

    1.时区转换(国际时间转换成本地时区时间)

    //转换时区 输入时间 输出+8时间
    +(NSDate *)convertDateToLocalTime:(NSDate *)forDate
    {
        
        NSTimeZone *nowTimeZone = [NSTimeZone localTimeZone];
        
        int timeOffset = [nowTimeZone secondsFromGMTForDate:forDate];
        
        NSDate *newDate = [forDate dateByAddingTimeInterval:timeOffset];
        
        return newDate;
    }
    

    2.日期格式转换(把20020912变成2002-09-12)

    + (NSString *)dateFormat:(NSString *)sourceString
    {
        if ([sourceString length] < 8) 
        {
            return @"";
        }
    
        NSString *timeString = sourceString;
        NSRange yearRange = NSMakeRange(0, 4);
        NSString *yearString = [timeString substringWithRange:yearRange];
        NSRange monthRange = NSMakeRange(4, 2);
        NSString *monthString = [timeString substringWithRange:monthRange];
        NSRange dayRange = NSMakeRange(6, 2);
        NSString *dayString = [timeString substringWithRange:dayRange];
        
        return [NSString stringWithFormat:@"%@-%@-%@",yearString,monthString,dayString];
    }
    

    3、时间格式转换(把160718变成16:07:18)

    + (NSString *)timeFormat:(NSString *)sourceString
    {
        if ([sourceString length] < 6) 
        {
            return sourceString;
        }
    
        NSString *timeString = sourceString;
    
        NSRange hourRange = NSMakeRange(0, 2);
        NSString *hourString = [timeString substringWithRange:hourRange];
    
        NSRange minuteRange = NSMakeRange(2, 2);
        NSString *minuteString = [timeString substringWithRange:minuteRange];
    
        NSRange secondRange = NSMakeRange(4, 2);
        NSString *secondString = [timeString substringWithRange:secondRange];
        
        return [NSString stringWithFormat:@"%@:%@:%@",hourString,minuteString,secondString];
    }
    

    4.计算7天内日期、星期,并按格式返回

    //自定义方法
    - (void)showDateViewWithMaxCount:(NSInteger)maxCount block:(void (^)(NSArray *))callBack
    {
        //日历初始化
        self.myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSMutableArray *dateMtbArr = [NSMutableArray array];
        NSDate *currentDate = [NSDate date];
        
        //  __今天____01-05
        NSString *curDateStr = [self dateToStringWithDate:currentDate];
        NSString *curMdStr = [[self dateToStringWithDate:currentDate] substringFromIndex:5];
        curMdStr = [curMdStr stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
        NSString *curStr = [NSString stringWithFormat:@"  今天    %@",curMdStr];
        [dateMtbArr addObject:@{@"showStr":curStr,
                                @"dateStr":curDateStr}];
        
        //计算本月天数是否够用
        NSInteger daysInMonth = [self getNumberOfDaysInMonthWithDate:currentDate];
        
        //本月要取天数
        NSInteger thisMonthDays = daysInMonth - [curDateStr substringFromIndex:7].integerValue;
        thisMonthDays = thisMonthDays >= maxCount - 1 ? maxCount - 1 : thisMonthDays;
        
        for (int i = 0 ; i < thisMonthDays; i ++)
        {
            //构造下一天字符串(_01-05__星期日)
            NSTimeInterval oneDay = 24 * 60 * 60;
            NSDate *nextDate = [NSDate dateWithTimeIntervalSinceNow:oneDay * (i + 1)];
            NSString *dateStr = [self dateToStringWithDate:nextDate];
            
            NSString *mdStr = [dateStr substringFromIndex:5];// 01/05
            mdStr = [mdStr stringByReplacingOccurrencesOfString:@"/" withString:@"-"];// 01-05
            NSString *weekStr = [self getweekDayWithDate:nextDate];// 星期日
            NSString *nextStr = [NSString stringWithFormat:@"%@  %@",weekStr,mdStr];
            
            [dateMtbArr addObject:@{@"showStr":nextStr,
                                    @"dateStr":dateStr}];
        }
        
        //下月要取天数
        if (thisMonthDays != maxCount - 1)
        {
            NSInteger nextMonthDays = maxCount - 1 - thisMonthDays;
            
            NSString *curYear = [curDateStr substringToIndex:4];
            NSString *curMonth = [curDateStr substringWithRange:NSMakeRange(5, 2)];
            NSString *nextYear = @"";
            NSString *nextMonth = @"";
            
            if (curMonth.integerValue == 12)
            {
                nextMonth = @"01";//改成01月   年+1
                nextYear = [NSString stringWithFormat:@"%d",curYear.intValue + 1];
            }
            else
            {
                nextMonth = [NSString stringWithFormat:@"%d",curMonth.intValue + 1];
                nextYear = curYear;
            }
            
            for (int i = 0; i < nextMonthDays; i ++)
            {
                NSString *dateStr = [NSString stringWithFormat:@"%@/%@/%d",nextYear,nextMonth,i + 1];
                NSDate *nextDate = [self stringToDateWithString:dateStr];
                
                NSString *mdStr = [dateStr substringFromIndex:5];// 01/05
                mdStr = [mdStr stringByReplacingOccurrencesOfString:@"/" withString:@"-"];// 01-05
                NSString *weekStr = [self getweekDayWithDate:nextDate];// 星期日
                NSString *nextStr = [NSString stringWithFormat:@"%@  %@",weekStr,mdStr];
                
                [dateMtbArr addObject:@{@"showStr":nextStr,
                                        @"dateStr":dateStr}];
            }
        }
        
        callBack([NSArray arrayWithArray:dateMtbArr]);
    }
    

    5、日期转换成字符串

    - (NSString *)dateToStringWithDate:(NSDate *)date
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy/MM/dd"]; //只获取年月日的数据
        return [formatter stringFromDate:date];
    }
    

    6、 字符串转换成日期

    - (NSDate *)stringToDateWithString:(NSString *)string
    {
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"yyyy/MM/dd"]; // 年/月/日
        return [formatter dateFromString:string];
    }
    

    7、 获取当月的天数

    - (NSInteger)getNumberOfDaysInMonthWithDate:(NSDate *)date
    {
        NSRange range = [self.myCalendar rangeOfUnit:NSDayCalendarUnit
                                            inUnit: NSMonthCalendarUnit
                                           forDate:date];
        return range.length;
    }
    

    8.获取指定的日期是星期几

    /**
     获取指定的日期是星期几
     @param date 日期
     @return 星期几
     */
    - (NSString *)getweekDayWithDate:(NSDate *)date
    {
        NSDateComponents *comps = [self.myCalendar components:NSWeekdayCalendarUnit fromDate:date];
        
        NSString *dateStr;
        
        // 1 是周日,2是周一 3.以此类推
        switch ([comps weekday])
        {
            case 1:
                dateStr = @"星期日";
                break;
            case 2:
                dateStr = @"星期一";
                break;
            case 3:
                dateStr = @"星期二";
                break;
            case 4:
                dateStr = @"星期三";
                break;
            case 5:
                dateStr = @"星期四";
                break;
            case 6:
                dateStr = @"星期五";
                break;
            case 7:
                dateStr = @"星期六";
                break;
            default:
                break;
        }
        
        return dateStr;
    }
    

    9、 获取指定日期的年,月,日,星期,时,分,秒信息

    /**
     *    NSCalendarUnitWeekday
     *     获取指定日期的年,月,日,星期,时,分,秒信息
     */
    - (void) getDateInfo
    {
        NSDate * date  = [NSDate date];
        NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法 NSCalendarIdentifierGregorian,NSGregorianCalendar
        // NSDateComponent 可以获得日期的详细信息,即日期的组成
        NSDateComponents *comps = [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:date];
    
        NSLog(@"年 = year = %ld",comps.year);
        NSLog(@"月 = month = %ld",comps.month);
        NSLog(@"日 = day = %ld",comps.day);
        NSLog(@"时 = hour = %ld",comps.hour);
        NSLog(@"分 = minute = %ld",comps.minute);
        NSLog(@"秒 = second = %ld",comps.second);
        NSLog(@"星期 =weekDay = %ld ",comps.weekday);
    }
    

    相关文章

      网友评论

          本文标题:NSDate

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