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. @weakify, @strongify ObjC的Block中使用weakSelf/strongSelf @weakify/@strongify

    首先要说说什么时候使用weakSelf和strongSelf. 下面引用一篇博客<到底什么时候才需要在ObjC的Block中使用weakSelf/strongSelf>的内容: Objec ...

  2. extjs 一些杂碎的技术问题

    1怎样将grid 的checkedbox 勾选状态都清除 inv.getSelectionModel().clearSelections(); inv.getView().refresh(); 2怎样 ...

  3. linux下解压war格式的包

    linux解压 .war 包 war格式的包可以解决web应用程序部署时候不用按照目录层次结构部署,而是将war包当作部署单元来使用. 下面就讲下怎么去解压 .war 格式的压缩包: 1.安装jdk, ...

  4. Duilib实现类似电脑管家扫描目录效果

    实现原理: 1.后台开线程遍历目录,遍历出一个文件路径在界面上更新显示(通过发消息通知主界面) 2.需要扩展一下Duilib控件,在此我扩展了CLabelUI,重写了PaintText函数 扩展控件的 ...

  5. Number To Indian Rupee Words in Oracle Forms / Reports

    Convert numbers to Indian Rupees format in Oracle Forms / Reports.Create the below mention function ...

  6. ubuntu14.04 64bit安装teamviewer

    1.下载teamviewer,链接如下: http://downloadus2.teamviewer.com/download/version_10x/teamviewer_10.0.36281_i3 ...

  7. Linux下文件重命名、创建、删除、修改及保存文件

    一.重命名(更名) linux 给文件改名的命令是mv命令 mv命令来为文件或目录改名或将文件由一个目录移入另一个目录中.该命令等同于DOS系统下的ren和move命令的组合.它的使用权限是所有用户. ...

  8. Deep Learning 16:用自编码器对数据进行降维_读论文“Reducing the Dimensionality of Data with Neural Networks”的笔记

    前言 论文“Reducing the Dimensionality of Data with Neural Networks”是深度学习鼻祖hinton于2006年发表于<SCIENCE > ...

  9. 解决:“java.lang.IllegalArgumentException: error at ::0 can't find referenced pointcut myMethod”问题!

    Spring版本:2.5.6 AspectJ是Spring自带的lib. Jdk版本:1.7.0_17 在配置没问题的情况下,报:java.lang.IllegalArgumentException: ...

  10. zju 1002

    // zju 1002 // #include "stdafx.h" #include <string> #include <iostream> using ...