NSDate增加分类 计算某年某月某日

#import <Foundation/Foundation.h>

 

@interface NSDate (Addition)

/**

 *某一天在当月多少号

 */

- (NSInteger)daysOfCurrentMonth;

/**

 *  某月第一天周几

 *

 */

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

}

 在制作日历是时间的分类尤其重要.

转载于:https://www.cnblogs.com/LVanswer/p/5687146.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值