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操作TexturePacker批量打包资源plist png

    import os,sys imagedir = 'D:\\PackerImg\\imgDir' outplistdir = 'D:\\PackerImg\\outDir' comend = 'Tex ...

  2. 2015弱校联盟(1) - B. Carries

    B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...

  3. ArrayList代码示例

    package com.shushine.framework.第七章Java标准类库;import java.util.ArrayList;/** * * <p> * 描述该类情况 {@l ...

  4. Hibernate中两种删除用户的方式

    第一种,是比较传统的,先根据主键列进行查询到用户,在进行删除用户 //删除数据 public void deleteStudent(String sno) { init() ; Student qu ...

  5. centos7 Nexus maven私有仓库

    cd /home wget http://www.sonatype.org/downloads/nexus-latest-bundle.tar.gz mkdir nexus tar zxvf nexu ...

  6. C#相等性比较

    本文阐述C#中相等性比较,其中主要集中在下面两个方面 ==和!=运算符,什么时候它们可以用于相等性比较,什么时候它们不适用,如果不使用,那么它们的替代方式是什么? 什么时候,需要自定一个类型的相等性比 ...

  7. 深入浅出设计模式——状态模式(State Pattern)

    模式动机 在很多情况下,一个对象的行为取决于一个或多个动态变化的属性,这样的属性叫做状态,这样的对象叫做有状态的 (stateful)对象,这样的对象状态是从事先定义好的一系列值中取出的.当一个这样的 ...

  8. 1074, "Column length too big for column 'err_solution' (max = 21845); use BLOB or TEXT instead"

    一个注意点,就是sqlalchemy 使用create_all()建表的时候,要在 create_all()所在页面import那些表的model sqlalchemy.exc.Operational ...

  9. linux 启动模式

    BLOS-->MBR(master boot record)主引导记录-->引导加载程序-->内核-->init process -->login

  10. win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)

    原文:http://www.cnblogs.com/naniannayue/archive/2012/11/19/2776948.html 要想成功架设SSL安全站点关键要具备以下几个条件. 1.需要 ...