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. 初识python第二天(3)

    我们接着上一篇博客,继续来来了解Python一些常见类的函数使用方法 一.int # 运算符,>=,比较self是否大于等于value,只要满足大于或者等于其中一个条件,就返回True,否则就返 ...

  2. 20145227&20145201 《信息安全系统设计基础》实验一 开发环境的熟悉

    北京电子科技学院(BESTI) 实 验 报 告 课程:信息安全系统设计基础 班级:1452 姓名:李子璇 鄢曼君 学号:20145201 20145227 成绩: 指导教师:娄嘉鹏 实验日期:2016 ...

  3. [问题2014S06] 复旦高等代数II(13级)每周一题(第六教学周)

    [问题2014S06]  试用有理标准型理论证明13级高等代数I期末考试最后一题: 设 \(V\) 为数域 \(K\) 上的 \(n\) 维线性空间,  \(\varphi\) 为 \(V\) 上的线 ...

  4. [问题2014S07] 复旦高等代数II(13级)每周一题(第七教学周)

    [问题2014S07]  设 \(A\in M_n(\mathbb{K})\) 在数域 \(\mathbb{K}\) 上的初等因子组为 \(P_1(\lambda)^{e_1},P_2(\lambda ...

  5. CentOS7 续

    网络环境 第一步:通过cmd查询自己本机的IP,然后记录下来,手工配置到本地连接上面IP:子网掩码:DNS1:101.7.8.9DNS2:202.38.184.13 第二步:给物理机 本地连接配置第二 ...

  6. 第九天 内容提供者 ContentResolver

    重点:理解ContentProvider 的作用和创建流程 1. 内容提供者,提供 其他数据库的访问. 特点       - 描述 : 它是android 四大组件之一,需要androidManife ...

  7. iOS 键盘类型定制归纳

    一.键盘风格 支持8种风格键盘. typedef enum { UIKeyboardTypeDefault, // 默认键盘:支持所有字符 UIKeyboardTypeASCIICapable, // ...

  8. Codeforces Round #383 (Div. 2) A,B,C,D 循环节,标记,暴力,并查集+分组背包

    A. Arpa’s hard exam and Mehrdad’s naive cheat time limit per test 1 second memory limit per test 256 ...

  9. XAF学习资源整合大全

    近期有很多XAF初学者与我联系,我多数时间在重复很多入门问题,所以决定整理一篇XAF资源列表,方便大家查找资料,也请知晓其他资源的人留言或与我联系,我将新资源追加到本篇文章中,方便更多人. 一.本博客 ...

  10. 《BI那点儿事》三国数据分析系列——蜀汉五虎上将与魏五子良将武力分析,绝对的经典分析

    献给广大的三国爱好者们,希望喜欢三国的朋友一起讨论,加深对传奇三国时代的了解 数据分析基础概念:集中趋势分析是指在大量测评数据分布中,测评数据向某点集中的情况.总体(population)是指客观存在 ...