美文网首页IOS Foundation库相关知识
iOS 关于时间的相关方法介绍

iOS 关于时间的相关方法介绍

作者: 小暖风 | 来源:发表于2017-08-19 14:16 被阅读4次

    1 //当前时间创建NSDate

    NSDate *myDate = [NSDate date];

    NSLog(@"myDate = %@",myDate);

    2 //从现在开始的24小时

    NSTimeInterval secondsPerDay = 24*60*60;

    NSDate *tomorrow = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

    NSLog(@"myDate = %@",tomorrow);

    3//根据已有日期创建日期

    NSTimeInterval secondsPerDay1 = 24*60*60;

    NSDate *now = [NSDate date];

    NSDate *yesterDay = [now addTimeInterval:secondsPerDay1];

    NSLog(@"yesterDay = %@",yesterDay);

    4//比较日期

    BOOL sameDate = [now isEqualToDate:yesterDay];

    NSLog(@"sameDate = %lu",sameDate);

    4.1//获取较早的日期

    NSDate *earlierDate = [yesterDay earlierDate:now];

    NSLog(@"earlierDate  = %@",earlierDate);

    4.2//较晚的日期

    NSDate *laterDate = [yesterDay laterDate:now];

    NSLog(@"laterDate  = %@",laterDate);

    //两个日期之间相隔多少秒

    NSTimeInterval secondsBetweenDates= [yesterDay timeIntervalSinceDate:now];

    NSLog(@"secondsBetweenDates=  %lf",secondsBetweenDates);

    //通过NSCALENDAR类来创建日期

    NSDateComponents *comp = [[NSDateComponents alloc]init];

    [comp setMonth:06];

    [comp setDay:01];

    [comp setYear:2001];

    NSCalendar *myCal = [[NSCalendaralloc]initWithCalendarIdentifier:NSGregorianCalendar];

    NSDate *myDate1 = [myCal dateFromComponents:comp];

    NSLog(@"myDate1 = %@",myDate1);

    //从已有日期获取日期

    NSCalendar * myCall = [NSCalendar alloc] initWithCalendarLdentifier:NSGregorianCalendar];

    NSDateComponents * comp1 = [myCalcomponents:NSMonthCalendarUnit | NSDayCalendarUnit | NSYearCalendarUnitfromDate:now];

    NSIntegermonth = [comp1 month];

    NSIntegeryear = [comp1 year];

    NSIntegerday = [comp1 day];

    //NSDateFormatter实现日期的输出

    NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

    [formatter setDateStyle:NSDateFormatterFullStyle];//直接输出的话是机器码

    //或者是手动设置样式[formatter setDateFormat:@"yyyy-mm-dd"];

    NSString *string = [formatter stringFromDate:now];

    NSLog(@"string = %@",string);

    NSLog(@"formater = %@",formatter);

    //获取日期格式对象

    - (NSDateFormatter *)dateFormatter {

    if (dateFormatter == nil) {

    dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateStyle:NSDateFormatterMediumStyle];

    [dateFormatter setTimeStyle:NSDateFormatterNoStyle];

    }

    return dateFormatter;

    }

    NSString与NSDate互相转换及NSDate比较

    由NSDate转换为NSString:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];

    NSLog(@"%@", strDate);

    [dateFormatter release];

    结果:

    2010-08-04 16:01:03

    由NSString转换为NSDate:

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

    NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];

    NSLog(@"%@", date);

    [dateFormatter release];

    结果:

    2010-08-04 16:01:03 +0800

    日期之间比较可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    与otherDate比较,相同返回YES

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较早的那个日期

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较晚的那个日期

    - (NSComparisonResult)compare:(NSDate *)other;

    该方法用于排序时调用:

    .当实例保存的日期值与anotherDate相同时返回NSOrderedSame

    .当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

    .当实例保存的日期值早于anotherDate时返回NSOrderedAscending

    只显示年月日

    NSDateComponents * componentOne = [[NSCalendar currentCalendar] components: NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendar:sm.createTime];

    NSString * timeStr = [NSStrin stringWithFormat:@"%d-%d-%d",componentOne.year,componentOne.month,componentOne.day];

    1.创建或初始化可用以下方法

    用于创建NSDate实例的类方法有

    + (id)date;

    返回当前时间

    + (id)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;

    返回以当前时间为基准,然后过了secs秒的时间

    + (id)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)secs;

    返回以2001/01/01 GMT为基准,然后过了secs秒的时间

    + (id)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;

    返回以1970/01/01 GMT为基准,然后过了secs秒的时间

    + (id)distantFuture;

    返回很多年以后的未来的某一天。(比如你需要一个比现在(Now)晚(大)很长时间的时间值,则可以调用该方法。测试返回了4000/12/31 16:00:00)

    + (id)distantPast;

    返回很多年以前的某一天。(比如你需要一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方法。测试返回了公元前0001/12/31 17:00:00)

    用于创建NSDate实例的实例方法有

    - (id)addTimeInterval:(NSTimeInterval)secs;

    返回以目前的实例中保存的时间为基准,然后过了secs秒的时间

    用于初始化NSDate实例的实例方法有

    - (id)init;

    初始化为当前时间。类似date方法

    初始化为以2001/01/01 GMT为基准,然后过了secs秒的时间。类似dateWithTimeIntervalSinceReferenceDate:方法

    - (id)initWithTimeInterval:(NSTimeInterval)secs sinceDate:(NSDate *)refDate;

    初始化为以refDate为基准,然后过了secs秒的时间

    - (id)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;

    初始化为以当前时间为基准,然后过了secs秒的时间

    2.日期之间比较可用以下方法

    - (BOOL)isEqualToDate:(NSDate *)otherDate;

    与otherDate比较,相同返回YES

    - (NSDate *)earlierDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较早的那个日期

    - (NSDate *)laterDate:(NSDate *)anotherDate;

    与anotherDate比较,返回较晚的那个日期

    - (NSComparisonResult)compare:(NSDate *)other;

    该方法用于排序时调用:

    .当实例保存的日期值与anotherDate相同时返回NSOrderedSame

    .当实例保存的日期值晚于anotherDate时返回NSOrderedDescending

    .当实例保存的日期值早于anotherDate时返回NSOrderedAscending

    3.取回时间间隔可用以下方法

    - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)refDate;

    以refDate为基准时间,返回实例保存的时间与refDate的时间间隔

    - (NSTimeInterval)timeIntervalSinceNow;

    以当前时间(Now)为基准时间,返回实例保存的时间与当前时间(Now)的时间间隔

    - (NSTimeInterval)timeIntervalSince1970;

    以1970/01/01 GMT为基准时间,返回实例保存的时间与1970/01/01 GMT的时间间隔

    - (NSTimeInterval)timeIntervalSinceReferenceDate;

    以2001/01/01 GMT为基准时间,返回实例保存的时间与2001/01/01 GMT的时间间隔

    + (NSTimeInterval)timeIntervalSinceReferenceDate;

    以2001/01/01 GMT为基准时间,返回当前时间(Now)与2001/01/01 GMT的时间间隔

    4.将时间表示成字符串

    - (NSString *)description;

    以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。(其中“±HHMM”表示与GMT的存在多少小时多少分钟的时区差异。比如,若时区设置在北京,则“±HHMM”显示为“+0800″)

    相关文章

      网友评论

        本文标题:iOS 关于时间的相关方法介绍

        本文链接:https://www.haomeiwen.com/subject/xretdxtx.html