#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