美文网首页
Object-C NSCalendar获取当前时间

Object-C NSCalendar获取当前时间

作者: 陈胜华 | 来源:发表于2016-10-25 10:20 被阅读29次

    获取当前月份有多少天

        NSCalendar *calendar1 = nil;
        
        if ([UIDevice currentDevice].systemVersion.doubleValue >= 8.0) {
            
            calendar1 = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
        } else {
            
            calendar1 = [NSCalendar currentCalendar];
        }
        
        NSRange rangeMonth = [calendar1 rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:[NSDate date]];
        
        NSUInteger numberOfDaysInMonth = rangeMonth.length;
     
        NSLog(@"当前月份 %@ 天",@(numberOfDaysInMonth));  
    

    获取当前年份和月份和天数

        NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
        
        unsigned unitFlags = NSCalendarUnitYear  |
                             NSCalendarUnitMonth |
                             NSCalendarUnitDay   |
                             NSCalendarUnitHour  |
                             NSCalendarUnitMinute|
                             NSCalendarUnitSecond|
                             NSCalendarUnitWeekday;
        
        NSDateComponents* components = [calendar components:unitFlags fromDate:[NSDate date]];
        NSInteger iCurYear = [components year]; //当前的年份
        NSInteger iCurMonth = [components month]; //当前的月份
        NSInteger iCurDay = [components day]; // 当前的号数
    

    相关文章

      网友评论

          本文标题:Object-C NSCalendar获取当前时间

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