【原】iOS学习之NSDate在项目中的一些类目扩展
在项目中,我们可能会面对各种各样的对于时间的需求,在这里提供几种可能会用到的需求代码
1、与今天的时间做比较,返回日期差值
代码:
- (NSInteger)compareWithToday {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSDate *today = [NSDate date];
NSString *todayStr = [dateFormatter stringFromDate:today];
today = [dateFormatter dateFromString:todayStr];
NSInteger interval = (NSInteger) [self timeIntervalSinceDate:today];
NSInteger intervalDate = ;
if (interval <= ) {
intervalDate = interval / ( * * ) - ;
} else {
intervalDate = interval / ( * * );
}
return intervalDate;
}
2、距离当前的时间间隔描述
代码:
- (NSString *)timeIntervalDescription
{
NSTimeInterval timeInterval = -[self timeIntervalSinceNow];
if (timeInterval < ) {
return @"1分钟内";
} else if (timeInterval < ) {
return [NSString stringWithFormat:@"%.f分钟前", timeInterval / ];
} else if (timeInterval < ) {
return [NSString stringWithFormat:@"%.f小时前", timeInterval / ];
} else if (timeInterval < ) {//30天内
return [NSString stringWithFormat:@"%.f天前", timeInterval / ];
} else if (timeInterval < ) {//30天至1年内
NSDateFormatter *dateFormatter = [NSDateFormatter dateFormatterWithFormat:@"M月d日"];
return [dateFormatter stringFromDate:self];
} else {
return [NSString stringWithFormat:@"%.f年前", timeInterval / ];
}
}
该方法主要用于计算时间距离当前时间的时间间隔描述,主要使用时间戳进行比较。思路是先计算出时间戳与当前时间的时间戳的差值,然后进行比较。
3、分解时间
代码:
#define DATE_COMPONENTS (NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekOfYear | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond | NSCalendarUnitWeekday | NSCalendarUnitWeekdayOrdinal)
#define CURRENT_CALENDAR [NSCalendar currentCalendar] - (NSInteger) nearestHour
{
NSTimeInterval aTimeInterval = [[NSDate date] timeIntervalSinceReferenceDate] + D_MINUTE * ;
NSDate *newDate = [NSDate dateWithTimeIntervalSinceReferenceDate:aTimeInterval];
NSDateComponents *components = [CURRENT_CALENDAR components:NSCalendarUnitHour fromDate:newDate];
return components.hour;
} - (NSInteger) hour
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.hour;
} - (NSInteger) minute
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.minute;
} - (NSInteger) seconds
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.second;
} - (NSInteger) day
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.day;
} - (NSInteger) month
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.month;
} - (NSInteger) week
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.weekOfMonth;
} - (NSInteger) weekday
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.weekday;
} - (NSInteger) nthWeekday // e.g. 2nd Tuesday of the month is 2
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.weekdayOrdinal;
} - (NSInteger) year
{
NSDateComponents *components = [CURRENT_CALENDAR components:DATE_COMPONENTS fromDate:self];
return components.year;
}
这些方法主要是用于将时间中某一个单位下的内容以数字的形式返回。
比如 2016-08-26 08:55:48 +0000 是零时区下的当前时间,
使用 - (NSInteger) year 方法的输出结果就是 2016;
使用 - (NSInteger) hour 方法的输出结果就是 当前时区的时间,我们在 8时区 ,结果就是 16;
使用 - (NSInteger) seconds 方法的输出结果就是 48 ...
【原】iOS学习之NSDate在项目中的一些类目扩展的更多相关文章
- Java Web学习系列——Maven Web项目中集成使用Spring、MyBatis实现对MySQL的数据访问
本篇内容还是建立在上一篇Java Web学习系列——Maven Web项目中集成使用Spring基础之上,对之前的Maven Web项目进行升级改造,实现对MySQL的数据访问. 添加依赖Jar包 这 ...
- 第15.27节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QTreeWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 树部件(Tree Widget)是Qt Designer中 Item Widgets(It ...
- vs2010 C# 如何将类做成DLL 再从另一个项目中使用这个类
vs2010 C# 如何将类做成DLL 再从另一个项目中使用这个类 2011-10-20 12:00 486人阅读 评论(0) 收藏 举报 一.将类做成DLL 方法一: 你可以通过在命令行下用命令将以 ...
- 第15.28节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QTableWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 表格部件为应用程序提供标准的表格显示工具,在表格内可以管理基于行和列的数据项,表格中的最大 ...
- 第15.26节 PyQt(Python+Qt)入门学习:Model/View架构中的便利类QListWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.概述 列表部件(List Widget)对应类QListWidget,是从QListView派生 ...
- Java项目中每一个类都可以有一个main方法
Java项目中每一个类都可以有一个main方法,但只有一个main方法会被执行,其他main方法可以对类进行单元测试. public class StaticTest { public static ...
- 编写Java程序,使用ThreadLocal类,项目中创建账户类 Account,类中包括账户名称name、 ThreadLocal 类的引用变量amount,表示存款
查看本章节 查看作业目录 需求说明: 某用户共有两张银行卡,账户名称相同,但卡号和余额不同.模拟用户使用这两张银行卡进行消费的过程,并打印出消费明细 实现思路: 项目中创建账户类 Account,类中 ...
- iOS开发之MVVM在项目中的应用
今天写这篇博客是想达到抛砖引玉的作用,想与大家交流一下思想,相互学习,博文中有不足之处还望大家批评指正.本篇博客的内容沿袭以往博客的风格,也是以干货为主,偶尔扯扯咸蛋(哈哈~不好好工作又开始发表博客啦 ...
- iOS开发多线程在实际项目中的运用
实际项目开发中为了能够给用户更好的体验,有些延时操作我们都会放在子线程中进行. 今天我们就来聊聊多线程在实际项目中的运用. 我们先来看看多线程的基础知识: 1.多线程的原理: 同一时间,CPU只能处理 ...
随机推荐
- jvm内存默认大小,及如何调整大小
jvm大小默认是64m,如果也要增大程序运行的内存,如果要调整JVM的大小,可以在run configuration中配置VM的参数 ,-Xmx100m表示配置其的大小为100M. 以下是一些配置的说 ...
- jquery_DOM笔记2
属性操作; addClass() 添加指定的类名.用于切换效果 after() 在元素后面插入 before() 在元素之前插入 append()在元素后面添加 appendTo() 一直在元素尾部添 ...
- 重写官方TodoList,对于初学react+redux的人来说,很有好处
虽然官网的TodoList的例子写的很详细,但是都是一步到位,就是给你一个action,好家伙,全部都写好了,给你一个reducer,所有功能也是都写好了,但是我们这些小白怎么可能一下就消化那么多,那 ...
- win7系统下 自带的定时关机
进入cmd下,输入shutdown -s -t 600 以上例子代表的是10分钟后自动关机 -s代表定时关机 -t代表着定时,时间以秒为单位一分钟60s 输入完后按enter 定时关机设置完成 当想取 ...
- python3的编码问题
Python3对文本(str)和二进制数据(bytes)作了更为清晰的区分. 文本默认是以Unicode编码(python2默认是ascii),由str类型表示,二进制数据则由bytes类型表示. s ...
- nginx配置反向代理解决前后端分离跨域问题
摘自<AngularJS深度剖析与最佳实践>P132 nginx配置文件如下: server { listen ; server_name your.domain.name; locati ...
- ASP.NET MVC 5 Jquery Validate
ClientValidationEnabled 在asp.net mvc 5中ClientValidationEnabled默认为TRUE,所以也不需要刻意去设置 应用ValidationAttrib ...
- MySQL interval()函数
INTERVAL(N,N1,N2,N3,..........) INTERVAL()函数进行比较列表(N,N1,N2,N3等等)中的N值.该函数如果N<N1返回0,如果N<N2返回1,如果 ...
- php二进制安全的含义
PHP里,有string的概念.string里,每个字符的大小为byte(与PHP相比,Java的每个字符为Character,是UTF8字符,C语言的每个字符可以在编译时选择). byte里,有AS ...
- ZOJ 3871 Convex Hull(计算几何、凸包)
题意:给n个点,|x[i]|,|y[i]| <= 1e9.求在所有情况下的子集下(子集点数>=3),凸包的面积和. 这题主要有几个方面,一个是凸包的面积,可以直接用线段的有向面积和求得,这 ...