Common NSDate methods (to be supplemented)

Source: Internet
Author: User

/**

** Determine whether the date is this week

**/

 

-(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]; // set the first day of a week to Monday.

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;

}

}

 

 

/***

* ** Calculate the number of weeks by date

****/

-(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 = @ "day ";

Break;

}

Case 1 :{

Week = @ "day ";

Break;

}

Case 2 :{

Week = @ "1 ";

Break;

}

Case 3 :{

Week = @ "2 ";

Break;

}

Case 4 :{

Week = @ "3 ";

Break;

}

Case 5 :{

Week = @ "4 ";

Break;

}

Case 6 :{

Week = @ "5 ";

Break;

}

Case 7 :{

Week = @ "6 ";

Break;

}

Default :{

Break;

}

};

Return week;

}


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.