
## DateTools
DateTools was written to streamline date and time handling in Objective-C. Classes and concepts from other languages served as an inspiration for DateTools, especially the [DateTime](http://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx) structure and [Time Period Library](Time Period Library) for .NET. Through these classes and others, DateTools removes the boilerplate required to access date components, handles more nuanced date comparisons, and serves as the foundation for entirely new concepts like Time Periods and their collections.
[](https://travis-ci.org/MatthewYork/DateTools)
[](http://cocoapods.org/?q=datetools)
####Featured In
<table>
<tr>
<td align="center">
<a href="https://itunes.apple.com/hk/app/yahoo-livetext-video-messenger/id995121875?mt=8"><img src="http://a4.mzstatic.com/us/r30/Purple2/v4/7c/1b/11/7c1b11f3-2a73-b655-751e-b21b2e8bc6f7/icon100x100.png" /></a>
</td>
<td align="center">
<a href="https://itunes.apple.com/app/id547436543"><img src="http://a5.mzstatic.com/us/r30/Purple5/v4/1f/7b/a5/1f7ba545-038e-353e-18a0-b6472eef1913/icon100x100.jpeg" /></a>
</td>
<td align="center">
<a href="https://itunes.apple.com/us/app/aldi-usa/id429396645?mt=8"><img src="http://a4.mzstatic.com/us/r30/Purple7/v4/a7/63/20/a76320db-2de4-62ad-b620-efaab8a179dc/icon100x100.jpeg" /></a>
</td>
<td align="center">
<a href="https://itunes.apple.com/us/app/guidebook/id428713847?mt=8"><img src="http://a5.mzstatic.com/us/r30/Purple7/v4/e4/af/db/e4afdbc1-9ceb-c403-4d06-299e7e693120/icon100x100.png" /></a>
</td>
<td align="center">
<a href="https://itunes.apple.com/us/app/pitch-locator-pro/id964965940?mt=8"><img src="http://a2.mzstatic.com/us/r30/Purple7/v4/39/ed/24/39ed248b-afab-ce8d-4276-35ba0459ac60/icon100x100.png" /></a>
</td>
<tr>
<td align="center">Yahoo! Livetext</td>
<td align="center">My Disney Experience</td>
<td align="center">ALDI</td>
<td align="center">Guidebook</td>
<td align="center">Pitch Locator Pro</td>
</tr>
</tr>
</table>
## Installation
**Cocoapods**
<code>pod 'DateTools'</code>
**Manual Installation**
All the classes required for DateTools are located in the DateTools folder in the root of this repository. They are listed below:
* <code>DateTools.h</code>
* <code>NSDate+DateTools.{h,m}</code>
* <code>DTConstants.h</code>
* <code>DTError.{h,m}</code>
* <code>DTTimePeriod.{h,m}</code>
* <code>DTTimePeriodGroup.{h,m}</code>
* <code>DTTimePeriodCollection.{h,m}</code>
* <code>DTTimePeriodChain.{h,m}</code>
The following bundle is necessary if you would like to support internationalization or would like to use the "Time Ago" functionality. You can add localizations at the `Localizations` subheading under `Info` in the `Project` menu.
* <code>DateTools.bundle</code>
<code>DateTools.h</code> contains the headers for all the other files. Import this if you want to link to the entire framework.
## Table of Contents
* [**NSDate+DateTools**](#nsdate-datetools)
* [Time Ago](#time-ago)
* [Date Components](#date-components)
* [Date Editing](#date-editing)
* [Date Comparison](#date-comparison)
* [Formatted Date Strings](#formatted-date-strings)
* [**Time Periods**](#time-periods)
* [Initialization](#initialization)
* [Time Period Info](#time-period-info)
* [Manipulation](#manipulation)
* [Relationships](#relationships)
* [**Time Period Groups**](#time-period-groups)
* [Time Period Collections](#time-period-collections)
* [Time Period Chains](#time-period-chains)
* [**Unit Tests**](#unit-tests)
* [**Credits**](#credits)
* [**License**](#license)
##NSDate+DateTools
One of the missions of DateTools was to make NSDate feel more complete. There are many other languages that allow direct access to information about dates from their date classes, but NSDate (sadly) does not. It safely works only in the Unix time offsets through the <code>timeIntervalSince...</code> methods for building dates and remains calendar agnostic. But that's not <i>always</i> what we want to do. Sometimes, we want to work with dates based on their date components (like year, month, day, etc) at a more abstract level. This is where DateTools comes in.
####Time Ago
No date library would be complete without the ability to quickly make an NSString based on how much earlier a date is than now. DateTools has you covered. These "time ago" strings come in a long and short form, with the latter closely resembling Twitter. You can get these strings like so:
```objc
NSDate *timeAgoDate = [NSDate dateWithTimeIntervalSinceNow:-4];
NSLog(@"Time Ago: %@", timeAgoDate.timeAgoSinceNow);
NSLog(@"Time Ago: %@", timeAgoDate.shortTimeAgoSinceNow);
//Output:
//Time Ago: 4 seconds ago
//Time Ago: 4s
```
Assuming you have added the localization to your project, `DateTools` currently supports the following languages:
- ar (Arabic)
- bg (Bulgarian)
- ca (Catalan)
- zh_Hans (Chinese Simplified)
- zh_Hant (Chinese Traditional)
- cs (Czech)
- da (Danish)
- nl (Dutch)
- en (English)
- fi (Finnish)
- fr (French)
- de (German)
- gre (Greek)
- gu (Gujarati)
- he (Hebrew)
- hi (Hindi)
- hu (Hungarian)
- is (Icelandic)
- id (Indonesian)
- it (Italian)
- ja (Japanese)
- ko (Korean)
- lv (Latvian)
- ms (Malay)
- nb (Norwegian)
- pl (Polish)
- pt (Portuguese)
- ro (Romanian)
- ru (Russian)
- sl (Slovenian)
- es (Spanish)
- sv (Swedish)
- th (Thai)
- tr (Turkish)
- uk (Ukrainian)
- vi (Vietnamese)
- cy (Welsh)
- hr (Croatian)
If you know a language not listed here, please consider submitting a translation. [Localization codes by language](http://stackoverflow.com/questions/3040677/locale-codes-for-iphone-lproj-folders).
This project is user driven (by people like you). Pull requests close faster than issues (merged or rejected).
Thanks to Kevin Lawler for his work on [NSDate+TimeAgo](https://github.com/kevinlawler/NSDate-TimeAgo), which has been officially merged into this library.
####Date Components
There is a lot of boilerplate associated with getting date components from an NSDate. You have to set up a calendar, use the desired flags for the components you want, and finally extract them out of the calendar.
With DateTools, this:
```objc
//Create calendar
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit;
NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];
//Get components
NSInteger year = dateComponents.year;
NSInteger month = dateComponents.month;
```
...becomes this:
```objc
NSInteger year = date.year;
NSInteger month = date.month;
```
And if you would like to use a non-Gregorian calendar, that option is available as well.
```objc
NSInteger day = [date dayWithCalendar:calendar];
```
If you would like to override the default calendar that DateTools uses, simply change it in the <code>defaultCalendar</code> method of <code>NSDate+DateTools.m</code>.
####Date Editing
The date editing methods in NSDate+DateTools makes it easy to shift a date earlier or later by adding and subtracting date components. For instance, if you would like a date that is 1 year later from a given date, simply call the method <code>dateByAddingYears</code>.
With DateTools, this:
```objc
//Create calendar
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:[NSDate defaultCalendar]];
NSDateComponents *components = [[NSDateComponents alloc] init];
//Make changes
[components setYear:1];
//Get new date with updated year
NSDate *newDate = [calendar dateByAddingComponents:components toDate:date options:0];
```
...becomes this:
```objc
NSDate *newDate = [date dateByAddingYears:1];
```
Subtraction of date components is

Coder_Kevin_Vans
- 粉丝: 727
- 资源: 73
最新资源
- 中科岩创隧道自动化监测解决方案
- Manatee 1.09电磁噪声振动计算软件:引领电机NVH领域革新,带教程易上手,超越同类软件的强大后处理功能,电机电磁振动噪声NVH解决利器:Manatee 1.09软件教程全面,强大后处理参数化
- C2000 MCU同步降压升压转换器的数字控制系统应用与优化
- .NET 9 彻底改变了 API 的文档:从 Swashbuckle 到 Scalar
- datax-mysql8驱动
- 气动影响下叶片裂纹应力集中现象的Fluent分析,基于气动影响的叶片裂纹应力集中现象的Fluent分析与研究,Fluent,考虑气动影响情况下的叶片裂纹应力集中 ,Fluent; 考虑气动影响; 叶片
- MATLAB驱动的高尔夫模拟仿真系统:深度定制球杆与挥杆参数的互动体验,基于MATLAB的全方位高尔夫模拟仿真系统:精确设定球杆与天气因素,让用户享受个性化的挥杆力量与角度掌控体验,基于MATLAB的
- 单向光伏并网逆变器:结构解析与性能追踪图集,包括整体结构图、并网电流电压曲线图、最大功率追踪MPPT控制图及直流母线电压曲线图,单向光伏并网逆变器:结构解析与性能追踪图集,含最大功率追踪图及电流电压曲
- COMSOL裂缝地层THM耦合离散模型与地热能开采过程研究:探究随机复杂裂缝对增强地热系统的影响,COMSOL裂缝地层THM耦合与离散随机复杂裂缝模型在地热能开采中的应用研究,COMSOL裂缝地层的T
- 基于C2000 MCU峰值电流控制模式的升压电路实现在电源领域的应用与优化
- site-packages.rar
- BUCK控制策略对比及主电路图详解:开环与闭环控制的波形与调节过程分析,BUCK控制策略对比及主电路图详解:开环与闭环控制的波形与调节过程分析,BUCK多种控制策略对比 图一BUCK主电路图与控制策略
- 永磁同步电机MATLAB仿真:直接转矩控制下的转速外环与磁链内环优化,转矩脉动显著减小,永磁同步电机MATLAB仿真:直接转矩控制下的转速外环与磁链内环优化,转矩脉动显著减小,永磁同步电机(PMSM)
- 清华大学最新学习教程《DeepSeek与AI幻觉》
- 销售数据集,数据集包含模拟不同产品、地区和客户的销售交易信息,可以用于机器学习
- 可以快速使用的一个日志模块,可以打印不同颜色,绑定到RichTextBox
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈


