http://blog.csdn.net/reylen/article/details/8560128

创建当前时间
NSDate *date = [NSDate date]; 从现在开始的24小时
NSTimeInterval a_day = **;
NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:a_day];
根据已有日期创建日期
[cpp] view plain copy print?
NSTimeInterval a_day = **;
NSDate *date = [NSDate date];
NSDate *tomorrow = [date addTimeInterval: date];
日期比较
[cpp] view plain copy print?
BOOL isEqual = [date isEqualToDate:tomorrow];
f(isEqual){
NSLog(@"equal"); //获取较早的日期
[cpp] view plain copy print?
NSDate *earlier_date = [tomorrow earlierDate:date];
NSLog(@"earlierDate = %@",earlierDate); //较晚的日期
[cpp] view plain copy print?
NSDate *laterDate = [tomorrow laterDate:date];
NSLog(@"laterDate = %@",laterDate); //两个日期之间相隔多少秒
[cpp] view plain copy print?
NSTimeInterval secondsInterval= [date timeIntervalSinceDate:tomorrow];
NSLog(@"secondsInterval= %lf",secondsInterval); //通过NSCalendar类来创建日期
[cpp] view plain copy print?
NSDateComponents *comps = [[NSDateComponentsalloc]init];
[comps setMonth:];
[comps setDay:];
[comps setYear:];
NSCalendar *calendar = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [calendar dateFromComponents:comps]; //从已有日期获取日期
[cpp] view plain copy print?
unsigned units = NSMonthCalendarUnit|NSDayCalendarUnit|NSYearCalendarUnit;
NSDateComponents *_comps = [calendar components:units fromDate:date];
NSInteger month = [_comps month];
NSInteger year = [_comps year];
NSInteger day = [_comps day]; //NSDateFormatter实现日期的输出
NSDateFormatter *formatter = [[NSDateFormatteralloc]init];
[formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码
//或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];
NSString *dateString = [formatter stringFromDate:date];
NSLog(@"dateString = %@",dateString);
NSLog(@"formater = %@",formatter); //获取日期格式对象
[cpp] view plain copy print?
- (NSDateFormatter *)getDateFormatter {
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}

时间比较

-(int)compareDate:(NSString*)date01 withDate:(NSString*)date02{
int ci;
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *dt1 = [[NSDate alloc] init];
NSDate *dt2 = [[NSDate alloc] init];
dt1 = [df dateFromString:date01];
dt2 = [df dateFromString:date02];
NSComparisonResult result = [dt1 compare:dt2];
switch (result)
{
//date02比date01大
case NSOrderedAscending: ci=; break;
//date02比date01小
case NSOrderedDescending: ci=-; break;
//date02=date01
case NSOrderedSame: ci=; break;
default: NSLog(@"erorr dates %@, %@", dt2, dt1); break;
}
return ci;
}

NSDate 时间比较...等的更多相关文章

  1. iOS - OC NSDate 时间

    前言 NSDate @interface NSDate : NSObject <NSCopying, NSSecureCoding> NSDate 用来表示公历的 GMT 时间(格林威治时 ...

  2. iOS - Swift NSDate 时间

    前言 NSDate public class NSDate : NSObject, NSCopying, NSSecureCoding NSDate 用来表示公历的 GMT 时间(格林威治时间).是独 ...

  3. NSdate 时间格式

    NSdate 时间格式 NSTimeInterval 时间间隔 基本单位 秒 NSDateFormatter 时间格式器 用于日期对象的格式化或字符串解析为日期对象 日期格式如下: y  年 M  年 ...

  4. NSDate时间类/NSDateFormatter日期格式类

    #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { // NSDate 时间类 继承自N ...

  5. NSDate 时间

    NSDate *date=[NSDate date]; NSDateFormatter *formatter=[[NSDateFormatter alloc]init]; formatter.date ...

  6. NSDate 时间加减

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/pearlhuzhu/article/details/26227393 NSDate有个类别,例如以下 ...

  7. NSDate时间

    NSDate 使用 ios时间的秒数 取当前时间的秒数 NSTimeInterval time = [[NSDate date] timeIntervalSince1970]; long long i ...

  8. iOS:NSDate的主要几种时间形式

    NSDate:时间的获取和操作 1.获取当前时间 //获取当前日期 NSDate *date = sender.date; NSLog(@"%@",date); 2.将date转换 ...

  9. OC - 时间日期类NSDate

    OC - 时间日期类NSDate //NSDate 时间日期类 NSDate 二进制数据流 { //1.获取当前时间 零时区的时间 //显示的是格林尼治的时间: 年-月-日 时:分:秒:+时区 NSD ...

随机推荐

  1. iOS性能优化:Instruments

    检测内存泄露:Instruments中的Leaks 前言 如果要检测内存泄露,我们会使用Xcode7自带的Instruments中的Leaks工具来检测. 现在的开发环境是ARC,所以很少会出现内存泄 ...

  2. linux查看修改线程默认栈空间大小(ulimit -s)

    linux查看修改线程默认栈空间大小 ulimit -s 1.通过命令 ulimit -s 查看linux的默认栈空间大小,默认情况下 为10240 即10M 2.通过命令 ulimit -s 设置大 ...

  3. GRUB学习笔记(转自http://www.cnblogs.com/evilzy/archive/2008/03/30/1130173.html)

    grub学习笔记1 首先要了解的几个概念 1.1 启动管理器 启动管理器是存储在磁盘开始扇区中的一段程序,例如,硬盘的MBR(Master Boot Record),在系统完成启动测试后,如果系统是从 ...

  4. 关于在官网上查看和下载特定版本的webrtc代码

    注:这个方法已经不适用了,帖子没删只是留个纪念而已 gclient:如果不知道gclient是什么东西 ... 就别再往下看了. 下载特定版本的代码: #gclient sync --revision ...

  5. (6) 深入理解Java Class文件格式(五)

    前情回顾 本专栏的前几篇博文, 对class文件中的常量池进行了详细的解释. 前文讲解了常量池中的7种数据项, 它们分别是: CONSTANT_Utf8_info CONSTANT_NameAndTy ...

  6. CentOS7 基础配置

    Centos 7 部分>>>>>>>>>>>>>>>>>>>>>>& ...

  7. 【转】CentOS下载版本介绍

    官网:http://www.centos.org/ 下载:http://mirror.neu.edu.cn/centos/6.6/isos/ 系统运维:http://www.osyunwei.com/ ...

  8. Javascript学习笔记:6种实现继承的方式

    ①原型链式继承 function Person(name){ this.name=name; } Person.prototype.getName=function(){ return this.na ...

  9. ELK 安装与配置

    ELK日志分析之安装 1.介绍: NRT elasticsearch是一个近似实时的搜索平台,从索引文档到可搜索有些延迟,通常为1秒. 集群 集群就是一个或多个节点存储数据,其中一个节点为主节点,这个 ...

  10. Linq排序,获取前5条数据

    _dic = _dic.OrderByDescending(x => x.Value).ToDictionary(x=>x.Key,x=>x.Value); var Num = _d ...