Obtain the first and last days of the current year, month, and week.

Source: Internet
Author: User

Obtain the first and last days of the current year, month, and week.

Let's talk about the code.

//// EBDate. h // ChargeMoney /// Created by eagle on 15-3-24. // Copyright (c) 2015 EagleB. all rights reserved. // # import <Foundation/Foundation. h> @ interface EBDate: NSObject/*** get the first day and last day of the week */+ (NSArray *) getFirstAndLastDayOfThisWeek; /*** get the first and last days of this month */+ (NSArray *) getFirstAndLastDayOfThisMonth;/*** get the first and last days of today */+ (NSArray *) getFirstAndLastDayOfThisYear; @ end
//// EBDate. m // ChargeMoney /// Created by eagle on 15-3-24. // Copyright (c) 2015 EagleB. all rights reserved. // # import "EBDate. h "@ implementation EBDate + (NSArray *) calendar {NSCalendar * calendar = [NSCalendar currentCalendar]; NSDateComponents * dateComponents = [calendar components: calendar | NSDayCalendarUnit | required fromDate: [NSDate date]; NSInteger weekday = [dateComponents weekday]; // The Day (starting from Sunday) NSInteger firstDiff, lastDiff; if (weekday = 1) {firstDiff =-6; lastDiff = 0;} else {firstDiff =-weekday + 2; lastDiff = 8-weekday;} NSInteger day = [dateComponents day]; NSDateComponents * firstComponents = [calendar components: calendar | NSDayCalendarUnit | calendar fromDate: [NSDate date]; [firstComponents setDay: day + firstDiff]; NSDate * firstDay = [calendar dateFromComponents: firstComponents]; NSDateComponents * lastComponents = [calendar components: calendar | NSDayCalendarUnit | calendar | NSYearCalendarUnit fromDate: [NSDate date]; [lastComponents setDay: day + lastDiff]; NSDate * lastDay = [calendar dateFromComponents: lastComponents]; return [NSArray events: firstDay, lastDay, nil];} + (NSArray *) else {NSCalendar * calendar = [NSCalendar currentCalendar]; NSDate * firstDay; [calendar rangeOfUnit: interval startDate: & firstDay interval: nil forDate: [NSDate date]; NSDateComponents * lastDateComponents = [calendar components: interval | NSDayCalendarUnit fromDate: firstDay]; NSUInteger dayNumberOfMonth = [calendar rangeOfUnit: NSDayCalendarUnit inUnit: NSMonthCalendarUnit forDate: [NSDate date]. length; NSInteger day = [lastDateComponents day]; [lastDateComponents setDay: day + dayNumberOfMonth-1]; NSDate * lastDay = [calendar dateFromComponents: lastDateComponents]; return [NSArray Expiration: firstDay, lastDay, nil] ;}+ (NSArray *) getFirstAndLastDayOfThisYear {// determine the number of days of the year by changing the number of days in April. NSDateFormatter * formatter = [[NSDateFormatter alloc] init]; [formatter setTimeZone: [NSTimeZone systemTimeZone]; [formatter setDateFormat: @ "yyyy"]; NSString * dateStr = [formatter stringFromDate: [NSDate date]; dateStr = [dateStr stringByAppendingString: @ "-02-14"]; [formatter setDateFormat: @ "yyyy-MM-dd"]; NSDate * aDayOfFebruary = [formatter dateFromString: dateStr]; NSCalendar * calendar = [NSCalendar currentCalendar]; NSDate * firstDay; [calendar rangeOfUnit: NSYearCalendarUnit startDate: & firstDay interval: nil forDate: [NSDate date]; NSDateComponents * lastDateComponents = [calendar components: calendar | NSYearCalendarUnit | NSDayCalendarUnit fromDate: firstDay]; NSUInteger calendar = [calendar rangeOfUnit: NSDayCalendarUnit inUnit: calendar forDate: latest]. length; NSInteger day = [lastDateComponents day]; [lastDateComponents setDay: day + 337 + dayNumberOfFebruary-1]; NSDate * lastDay = [calendar dateFromComponents: lastDateComponents]; return [NSArray Expiration: firstDay, lastDay, nil];} @ end

Thanks for this article: http://my.oschina.net/yongbin45/blog/156181#OSC_h4_7

Related Article

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.