Nsdate的各种常用操作
//
// 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的各种常用操作的更多相关文章
- 【三】用Markdown写blog的常用操作
本系列有五篇:分别是 [一]Ubuntu14.04+Jekyll+Github Pages搭建静态博客:主要是安装方面 [二]jekyll 的使用 :主要是jekyll的配置 [三]Markdown+ ...
- php模拟数据库常用操作效果
test.php <?php header("Content-type:text/html;charset='utf8'"); error_reporting(E_ALL); ...
- Mac OS X常用操作入门指南
前两天入手一个Macbook air,在装软件过程中摸索了一些基本操作,现就常用操作进行总结, 1关于触控板: 按下(不区分左右) =鼠标左键 control+按下 ...
- mysql常用操作语句
mysql常用操作语句 1.mysql -u root -p 2.mysql -h localhost -u root -p database_name 2.列出数据库: 1.show datab ...
- nodejs配置及cmd常用操作
一.cmd常用操作 1.返回根目录cd\ 2.返回上层目录cd .. 3.查找当前目录下的所有文件dir 4.查找下层目录cd window 二.nodejs配置 Node.js安装包及源码下载地址为 ...
- Oracle常用操作——创建表空间、临时表空间、创建表分区、创建索引、锁表处理
摘要:Oracle数据库的库表常用操作:创建与添加表空间.临时表空间.创建表分区.创建索引.锁表处理 1.表空间 ■ 详细查看表空间使用状况,包括总大小,使用空间,使用率,剩余空间 --详细查看表空 ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- byte数据的常用操作函数[转发]
/// <summary> /// 本类提供了对byte数据的常用操作函数 /// </summary> public class ByteUtil { ','A','B',' ...
- Linux Shell数组常用操作详解
Linux Shell数组常用操作详解 1数组定义: declare -a 数组名 数组名=(元素1 元素2 元素3 ) declare -a array array=( ) 数组用小括号括起,数组元 ...
随机推荐
- delphi android 中 Toast 的实现(老外写的UNIT)
unit Android.JNI.Toast; // Java bridge class imported by hand by Brian Long (http://blong.com)interf ...
- 解决在sublime text3在ubuntu下无法输入中文的问题
方法链接:https://github.com/lyfeyaj/sublime-text-imfix 效果图:
- Python学习教程(learning Python)--3.3 分支语句的条件表达式详解
本节主要讨论分支语句的条件表达式问题. 在if或者if-else分支控制语句里由于都用到条件判断(表达式是真还是假),条件判断可以是一种关系运算也可以是布尔表达式. 本节将对if及if-else语句的 ...
- angularjs2 学习笔记(六) Form
Angular 2 Form表单 在angular2 form表单中我们需要了解表单数据绑定.数据验证.数据提交等内容,在下面的示例中并没有实际提交到后台,这部分内容在今后webapi中加以练习. 表 ...
- 算法系列3《SHA》
SHA是一种数据加密算法,该算法经过加密专家多年来的发展和改进已日益完善,现在已成为公认的最安全的散列算法之一,并被广泛使用.该算法的思想是接收一段明文,然后以一种不可逆的方式将它转换成一段(通常更小 ...
- DB2中的转义字符
1.数据库脚本 )); ,'20%'); ,'OLIVER_QIN'); ,'AA''') 2.以下是DB2的转义字符 2.1 对“%”的转义 SELECT * FROM OLIVER_11 WHER ...
- ode.js 版本控制 nvm 和 n 使用 及 nvm 重启终端失效的解决方法
今天的话题包括2个部分 node.js 下使用 nvm 或者 n 来进行版本控制 nvm 安装node.js 版本后,重启终端 node , npm 环境变量失效 第一部分 用什么来管理 node.j ...
- Linux rsync 同步
rsync 是一个快速增量文件传输工具,它可以用于在同一主机备份内部的备分,我们还可以把它作为不同主机网络备份工具之用.本文主要讲述的是如何自架rsync服务器,以实现文件传输.备份和镜像.相对tar ...
- svn merge和branch
http://www.cnblogs.com/cxd4321/archive/2012/07/12/2588110.html 使用svn几年了,一直对分支和合并敬而远之,一来是因为分支的管理不该我操心 ...
- Swing做的非阻塞式仿飞秋聊天程序
采用Swing 布局 NIO非阻塞式仿飞秋聊天程序, 切换皮肤颜色什么的小功能以后慢慢做 启动主程序. 当用户打开主程序后自动获取局域网段IP可以在 设置 --> IP网段过滤, 拥有 JMF ...