当前位置:编程学习 > wap >>

NSDate 一些常用方法(待补充)

/**

 **判断 日期是否是本周

 **/

 

-(BOOL)isCurrentWeek:(NSString *)dateStr{

    NSDateFormatter *dateFormatter=[[NSDateFormatteralloc]init];

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

    NSDate *date=[dateFormatter dateFromString:dateStr];

    [dateFormatter release];

       

        NSDate *start;

        NSTimeInterval extends;

       

        NSCalendar *cal=[NSCalendarautoupdatingCurrentCalendar];

       [cal setFirstWeekday:2];//一周的第一天设置为周一

        NSDate *today=[NSDatedate];

       

        BOOL success= [calrangeOfUnit:NSWeekCalendarUnit startDate:&start interval: &extends forDate:today];

       

        if(!success)

            return NO;

       

        NSTimeInterval dateInSecs = [date timeIntervalSinceReferenceDate];

        NSTimeInterval dayStartInSecs= [starttimeIntervalSinceReferenceDate];

       

        if(dateInSecs >= dayStartInSecs && dateInSecs < (dayStartInSecs+extends)){

            return YES;

        } 

        else { 

            return NO; 

        } 

   

}

 

 

/***

 ***根据日期计算周几

 ****/

-(NSString *)weekDayWithDate:(NSString *)date{

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

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

    NSDate *fromdate=[dateFormatter dateFromString:date];

   

    NSCalendar *gregorian = [NSCalendar currentCalendar];

    NSDateComponents *weekDayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:fromdate];

    NSInteger mDay = [weekDayComponents weekday];

    [dateFormatter release];

    NSString *week=@"";

    switch (mDay) {

        case 0:{

            week=@"日";

            break;

        }

        case 1:{

            week=@"日";

            break;

        }

        case 2:{

            week=@"一";

            break;

        }

        case 3:{

            week=@"二";

            break;

        }

        case 4:{

            week=@"三";

            break;

        }

        case 5:{

            week=@"四";

            break;

        }

        case 6:{

            week=@"五";

            break;

        }

        case 7:{

            week=@"六";

            break;

        }

        default:{

            break;

        }

    };

    return week;

}


 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,