NSDate increase classification calculate a certain day of the year

Source: Internet
Author: User
Tags dateformat

#import <Foundation/Foundation.h>

@interface NSDate (addition)

/**

* How many numbers a day in the month

*/

-(Nsinteger) Daysofcurrentmonth;

/**

* The first day of the month of the week

*

*/

-(Nsinteger) Weekoffirstdayincurrentmonth;

-(Nsinteger) day;

-(Nsinteger) month;

-(Nsinteger) year;

-(NSString *) Datestrwithformat: (NSString *) format;

-(Nstimeinterval) Zerotimestamp;

@end

#import "Nsdate+addition.h"

@implementation NSDate (addition)

-(Nsinteger) Daysofcurrentmonth

{

Nsinteger days = 0;

Nscalendar *calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian];

days = [Calendar Rangeofunit:nscalendarunitday inunit:nscalendarunitmonth fordate:self].length;

return days;

}

-(Nsinteger) Weekoffirstdayincurrentmonth

{

Nsinteger week = 0;

Nscalendar *calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian];

Week = [Calendar Ordinalityofunit:nscalendarunitday inunit:nscalendarunitweekofmonth fordate:self]-1;

Return week;

}

-(Nsinteger) Day

{

Nsinteger day = 1;

Nscalendar *calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian];

Day = [Calendar Ordinalityofunit:nscalendarunitday inunit:nscalendarunitmonth fordate:self];

Return day;

}

-(Nsinteger) month

{

Nsinteger month = 1;

Nscalendar *calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian];

month = [Calendar Ordinalityofunit:nscalendarunitmonth inunit:nscalendarunityear fordate:self];

Return month;

}

-(Nsinteger) year

{

Nsinteger year = 2015;

Nscalendar *calendar = [[Nscalendar alloc] Initwithcalendaridentifier:nscalendaridentifiergregorian];

Year = [Calendar Ordinalityofunit:nscalendarunityear inunit:nscalendarunitera fordate:self];

Return year;

}

-(NSString *) Datestrwithformat: (NSString *) format

{

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

Formatter.dateformat = format;

return [formatter stringfromdate:self];

}

-(Nstimeinterval) Zerotimestamp

{

Nstimeinterval timeinterval = 0;

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

Formatter.dateformat = @ "YYYY-MM-DD";

NSString *zerotimestr = [Formatter stringfromdate:self];

NSDate *zerodate = [Formatter datefromstring:zerotimestr];

TimeInterval = [Zerodate timeIntervalSince1970];

return timeinterval;

}

It is especially important to make the calendar a time-of-day classification.

NSDate increase classification calculate a certain day of the year

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.