//

//  NVDate.h

//

//  Created by Noval Agung Prayogo on 2/5/14.

//  Copyright (c) 2014 Noval Agung Prayogo. All rights reserved.

//

#import <Foundation/Foundation.h>

typedef NS_OPTIONS(NSUInteger, NVDayUnit) {

NVDayUnitSunday    = 1,

NVDayUnitMonday    = 2,

NVDayUnitTuesday   = 3,

NVDayUnitWednesday = 4,

NVDayUnitThursday  = 5,

NVDayUnitFriday    = 6,

NVDayUnitSaturday  = 7

};

typedef NS_OPTIONS(NSUInteger, NVMonthUnit) {

NVMonthUnitJanuary   = 1,

NVMonthUnitFebruary  = 2,

NVMonthUnitMarch     = 3,

NVMonthUnitApril     = 4,

NVMonthUnitMay       = 5,

NVMonthUnitJune      = 6,

NVMonthUnitJuly      = 7,

NVMonthUnitAugust    = 8,

NVMonthUnitSeptember = 9,

NVMonthUnitOctober   = 10,

NVMonthUnitNovember  = 11,

NVMonthUnitDecember  = 12,

};

@interface NVDate : NSObject

- (id)initUsingToday;

- (id)initUsingDate:(NSDate *)date;

- (id)initUsingYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day;

- (id)initUsingYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second;

- (id)initUsingString:(NSString *)stringDate;

- (id)initUsingString:(NSString *)stringDate withFormat:(NSString *)dateFormat;

- (id)initUsingSeconds:(NSInteger)seconds;

- (NSDate *)date;

- (NSString *)stringValue;

- (NSString *)stringValueWithFormat:(NSString *)dateFormat;

- (void)zeroTime;

- (instancetype)previousDay;

- (instancetype)previousDays:(NSInteger)days;

- (instancetype)nextDay;

- (instancetype)nextDays:(NSInteger)days;

- (instancetype)previousWeek;

- (instancetype)previousWeeks:(NSInteger)weeks;

- (instancetype)nextWeek;

- (instancetype)nextWeeks:(NSInteger)weeks;

- (instancetype)previousMonth;

- (instancetype)previousMonths:(NSInteger)months;

- (instancetype)nextMonth;

- (instancetype)nextMonths:(NSInteger)months;

- (instancetype)previousYear;

- (instancetype)previousYears:(NSInteger)years;

- (instancetype)nextYear;

- (instancetype)nextYears:(NSInteger)years;

- (instancetype)firstDayOfMonth;

- (instancetype)lastDayOfMonth;

- (instancetype)firstMonthOfYear;

- (instancetype)lastMonthOfYear;

- (instancetype)previousDayOfDayName:(NVDayUnit)dayUnit;

- (instancetype)nextDayOfDayName:(NVDayUnit)dayUnit;

- (BOOL)isCurrentDayName:(NVDayUnit)dayUnit;

- (BOOL)isCurrentMonthName:(NVMonthUnit)monthUnit;

@property NSString *dateFormatUsingString;

@property NSDateFormatterStyle dateFormatUsingStyle;

@property NSDateFormatterStyle timeFormatUsingStyle;

@property NSInteger year;

@property NSInteger month;

@property (readonly) NSInteger week;

@property NSInteger day;

@property NSInteger hour;

@property NSInteger minute;

@property NSInteger second;

@end

//

//  NVDate.m

//

//  Created by Noval Agung Prayogo on 2/5/14.

//  Copyright (c) 2014 Noval Agung Prayogo. All rights reserved.

//

#import "NVDate.h"

@implementation NVDate {

NSDateFormatter *_dateFormatter;

NSCalendar *_calendar;

NSDate *_date;

NSCalendarUnit _dateTimeCalendarUnit;

}

- (id)init {

if (self = [super init]) {

_dateFormatter = [[NSDateFormatter alloc] init];

_dateFormatter.dateStyle = NSDateFormatterFullStyle;

_dateFormatter.timeStyle = NSDateFormatterFullStyle;

_calendar = [NSCalendar currentCalendar];

_date = [[NSDate alloc] init];

_dateTimeCalendarUnit = (NSYearCalendarUnit | NSMonthCalendarUnit | NSWeekCalendarUnit | NSWeekdayCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit);

}

returnself;

}

- (id)initUsingToday {

if (self = [self init]) {

_date = [NSDate date];

}

returnself;

}

- (id)initUsingString:(NSString *)stringDate {

if (self = [self init]) {

_date = [_dateFormatter dateFromString:stringDate];

}

returnself;

}

- (id)initUsingString:(NSString *)stringDate withFormat:(NSString *)dateFormat {

if (self = [self init]) {

_dateFormatter.dateFormat = dateFormat;

_date = [_dateFormatter dateFromString:stringDate];

}

returnself;

}

- (id)initUsingYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day {

if (self = [self init]) {

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = year;

dateComponents.month = month;

dateComponents.day = day;

_date = [_calendar dateFromComponents:dateComponents];

}

returnself;

}

- (id)initUsingYear:(NSInteger)year month:(NSInteger)month day:(NSInteger)day hour:(NSInteger)hour minute:(NSInteger)minute second:(NSInteger)second {

if (self = [self init]) {

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = year;

dateComponents.month = month;

dateComponents.day = day;

dateComponents.hour = hour;

dateComponents.minute = minute;

dateComponents.second = second;

_date = [_calendar dateFromComponents:dateComponents];

}

returnself;

}

- (id)initUsingDate:(NSDate *)date {

if (self = [self init]) {

_date = date;

}

returnself;

}

- (id)initUsingSeconds:(NSInteger)seconds {

if (self = [self init]) {

_date = [NSDate dateWithTimeIntervalSinceReferenceDate:seconds];

}

returnself;

}

- (NSDate *)date {

return _date;

}

- (NSString *)stringValue {

return [_dateFormatter stringFromDate:_date];

}

- (NSString *)stringValueWithFormat:(NSString *)dateFormat {

NSDateFormatter *localDateFormatter = [_dateFormatter copy];

localDateFormatter.dateFormat = dateFormat;

return [localDateFormatter stringFromDate:_date];

}

- (void)zeroTime {

NSDateComponents *dateComponents = [_calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit) fromDate:_date];

dateComponents.hour = 0;

dateComponents.minute = 0;

dateComponents.second = 0;

_date = [_calendar dateFromComponents:dateComponents];

}

- (void)setDateFormatUsingString:(NSString *)dateFormatUsingString {

_dateFormatter.dateFormat = dateFormatUsingString;

}

- (NSString *)dateFormatUsingString {

return _dateFormatter.dateFormat;

}

- (void)setDateFormatUsingStyle:(NSDateFormatterStyle)dateFormatUsingStyle {

_dateFormatter.dateStyle = dateFormatUsingStyle;

}

- (NSDateFormatterStyle)dateFormatUsingStyle {

return _dateFormatter.dateStyle;

}

- (void)setTimeFormatUsingStyle:(NSDateFormatterStyle)timeFormatUsingStyle {

_dateFormatter.timeStyle = timeFormatUsingStyle;

}

- (NSDateFormatterStyle)timeFormatUsingStyle {

return _dateFormatter.timeStyle;

}

- (instancetype)daysCalculate:(NSInteger)days isForward:(BOOL)isForward {

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.day = days * (isForward ? 1 : -1);

_date = [_calendar dateByAddingComponents:dateComponents toDate:_date options:0];

returnself;

}

- (instancetype)previousDay {

return [self previousDays:1];

}

- (instancetype)previousDays:(NSInteger)days {

return [self daysCalculate:days isForward:NO];

}

- (instancetype)nextDay {

return [self nextDays:1];

}

- (instancetype)nextDays:(NSInteger)days {

return [self daysCalculate:days isForward:YES];

}

- (instancetype)weeksCalculate:(NSInteger)weeks isForward:(BOOL)isForward {

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.day = (7 * weeks) * (isForward ? 1 : -1);

_date = [_calendar dateByAddingComponents:dateComponents toDate:_date options:0];

returnself;

}

- (instancetype)previousWeek {

return [self previousWeeks:1];

}

- (instancetype)previousWeeks:(NSInteger)weeks {

return [self weeksCalculate:weeks isForward:NO];

}

- (instancetype)nextWeek {

return [self nextWeeks:1];

}

- (instancetype)nextWeeks:(NSInteger)weeks {

return [self weeksCalculate:weeks isForward:YES];

}

- (instancetype)monthsCalculate:(NSInteger)months isForward:(BOOL)isForward {

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.month = months * (isForward ? 1 : -1);

_date = [_calendar dateByAddingComponents:dateComponents toDate:_date options:0];

returnself;

}

- (instancetype)previousMonth {

return [self previousMonths:1];

}

- (instancetype)previousMonths:(NSInteger)months {

return [self monthsCalculate:months isForward:NO];

}

- (instancetype)nextMonth {

return [self nextMonths:1];

}

- (instancetype)nextMonths:(NSInteger)months {

return [self monthsCalculate:months isForward:YES];

}

- (instancetype)yearsCalculate:(NSInteger)years isForward:(BOOL)isForward {

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

dateComponents.year = years * (isForward ? 1 : -1);

_date = [_calendar dateByAddingComponents:dateComponents toDate:_date options:0];

returnself;

}

- (instancetype)previousYear {

return [self previousYears:1];

}

- (instancetype)previousYears:(NSInteger)years {

return [self yearsCalculate:years isForward:NO];

}

- (instancetype)nextYear {

return [self nextYears:1];

}

- (instancetype)nextYears:(NSInteger)years {

return [self yearsCalculate:years isForward:YES];

}

- (instancetype)firstDayOfMonth {

NSDateComponents *dateComponents = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

dateComponents.day = 1;

_date = [_calendar dateFromComponents:dateComponents];

returnself;

}

- (instancetype)lastDayOfMonth {

NSDateComponents *dateComponents;

NSDate *date;

dateComponents = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

dateComponents.day = 1;

date = [_calendar dateFromComponents:dateComponents];

dateComponents = [[NSDateComponents alloc] init];

dateComponents.month = 1;

date = [_calendar dateByAddingComponents:dateComponents toDate:date options:0];

dateComponents = [[NSDateComponents alloc] init];

dateComponents.day = -1;

_date = [_calendar dateByAddingComponents:dateComponents toDate:date options:0];

returnself;

}

- (instancetype)firstMonthOfYear {

NSDateComponents *dateComponents = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

dateComponents.month = NVMonthUnitJanuary;

_date = [_calendar dateFromComponents:dateComponents];

returnself;

}

- (instancetype)lastMonthOfYear {

NSDateComponents *dateComponents = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

dateComponents.month = NVMonthUnitDecember;

_date = [_calendar dateFromComponents:dateComponents];

returnself;

}

- (instancetype)previousDayOfDayName:(NVDayUnit)dayUnit {

int currentWeekDay = [_calendar components:_dateTimeCalendarUnit fromDate:_date].weekday;

if (currentWeekDay == dayUnit)

return [self previousWeek];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

if (currentWeekDay > dayUnit)

dateComponents.day = -(currentWeekDay - dayUnit);

else

dateComponents.day = -currentWeekDay - (7 - dayUnit);

_date = [_calendar dateByAddingComponents:dateComponents toDate:_date options:0];

returnself;

}

- (instancetype)nextDayOfDayName:(NVDayUnit)dayUnit {

int currentWeekDay = [_calendar components:_dateTimeCalendarUnit fromDate:_date].weekday;

if (currentWeekDay == dayUnit)

return [self nextWeek];

NSDateComponents *dateComponents = [[NSDateComponents alloc] init];

if (currentWeekDay < dayUnit)

dateComponents.day = dayUnit - currentWeekDay;

else

dateComponents.day = -currentWeekDay + dayUnit;

_date = [_calendar dateByAddingComponents:dateComponents toDate:_date options:0];

returnself;

}

- (BOOL)isCurrentDayName:(NVDayUnit)dayUnit {

return ([_calendar components:_dateTimeCalendarUnit fromDate:_date].weekday == dayUnit);

}

- (BOOL)isCurrentMonthName:(NVMonthUnit)monthUnit {

return ([_calendar components:_dateTimeCalendarUnit fromDate:_date].month == monthUnit);

}

- (NSInteger)year {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].year;

}

- (void)setYear:(NSInteger)year {

NSDateComponents *components = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

components.year = year;

_date = [_calendar dateFromComponents:components];

}

- (NSInteger)month {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].month;

}

- (void)setMonth:(NSInteger)month {

NSDateComponents *components = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

components.month = month;

_date = [_calendar dateFromComponents:components];

}

- (NSInteger)week {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].week;

}

- (NSInteger)day {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].day;

}

- (void)setDay:(NSInteger)day {

NSDateComponents *components = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

components.day = day;

_date = [_calendar dateFromComponents:components];

}

- (NSInteger)hour {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].hour;

}

- (void)setHour:(NSInteger)hour {

NSDateComponents *components = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

components.hour = hour;

_date = [_calendar dateFromComponents:components];

}

- (NSInteger)minute {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].minute;

}

- (void)setMinute:(NSInteger)minute {

NSDateComponents *components = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

components.minute = minute;

_date = [_calendar dateFromComponents:components];

}

- (NSInteger)second {

return [_calendar components:_dateTimeCalendarUnit fromDate:_date].second;

}

- (void)setSecond:(NSInteger)second {

NSDateComponents *components = [_calendar components:_dateTimeCalendarUnit fromDate:_date];

components.second = second;

_date = [_calendar dateFromComponents:components];

}

@end

Nsdate的各种常用操作的更多相关文章

  1. 【三】用Markdown写blog的常用操作

    本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...

  2. php模拟数据库常用操作效果

    test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...

  3. Mac OS X常用操作入门指南

    前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右)            =鼠标左键 control+按下        ...

  4. mysql常用操作语句

    mysql常用操作语句 1.mysql -u root -p   2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...

  5. nodejs配置及cmd常用操作

    一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...

  6. Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理

    摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■  详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...

  7. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  8. byte数据的常用操作函数[转发]

    /// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...

  9. Linux Shell数组常用操作详解

    Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...

随机推荐

  1. 如何解决加载动态链接库DLL失败,返回0,GetLastError返回错误码126

    通常情况下使用LoadLibrary加载DLL都可以成功,但是当被加载的DLL内部依赖其他DLL无法被找到时,该函数会返回126(ERROR_MOD_NOT_FOUND)错误. 解决办法有2种: 1) ...

  2. Oracle存储过程学习备忘

    之前的项目使用存储过程很少,但在实际的项目中,存储过程的使用是必不可少的. 存储过程是一组为了完成特定功能的SQL 语句 集,经编译后存储在数据库中:存储过程创建后,一次编译在程序中可以多次调用,对安 ...

  3. 安装wampserver出现“The Program can't start because MSVCR110.dll is missing from your computer. Try reinstalling the program to fix this problem”

    问题描述: 今天安装wampserver的时候出现"The Program can't start because MSVCR110.dll is missing from your com ...

  4. 9.python的布尔类型与流程控制

    布尔类型其实可以算是一种特殊的数字,下面是 help() 函数得到的帮助信息: Help on class bool in module __builtin__: class bool(int) | ...

  5. 刀哥多线程Barrier异步gcd-08-barrier_async

    Barrier 异步 主要用于在多个异步操作完成之后,统一对非线程安全的对象进行更新 适合于大规模的 I/O 操作 代码演练 准备工作 @interface ViewController () { / ...

  6. oracle11g 新特性 - rman自动备份控制文件延迟

    OS: Oracle Linux Server release 5.7 DB: Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 -6 ...

  7. oracle 修改表空间存储路径

    [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 Oracle Database 11g Enterpris ...

  8. golang的内存模型与new()与make()

    要彻底理解new()与make()的区别, 最好从内存模型入手. golang属于c family, 而c程序在unix的内在模型: |低地址|text|data|bss|heap-->|unu ...

  9. 用Swift重写公司OC项目(Day1)--程序的AppIcon与LaunchImage如何设置

    公司之前的APP呢经过了两次重写,都是使用OC由本人独立开发的,不过这些东西我都不好意思说是自己写的,真心的一个字:丑!!! 客观原因来说主要是公司要的特别急,而且注重的是功能而非效果,公司的美工之前 ...

  10. 【EF Code First】Migrations数据库迁移

    1,打开工具->NuGet程序管理器->程序包管理器控制台 默认项目中要选择  数据访问上下文类  所在的项目 我的DB是在命名空间CodeFirst.UI下的所以选择CodeFirst. ...