你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> NSDate 一些常用方法(待補充)

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

編輯:關於IOS

/**

 **判斷 日期是否是本周

 **/

 

-(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;

}

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved