zoukankan      html  css  js  c++  java
  • iOS NSString与NSDate互相转换及NSDate比较

    由 NSDate 转换为 NSString:

    1. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    2. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    3. NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
    4. NSLog(@"%@", strDate);

    结果:

    1. 2015-06-14 13:01:03

    由 NSString 转换为 NSDate:

    1. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    2. [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    3. NSDate *date = [dateFormatter dateFromString:@"2010-08-04 16:01:03"];
    4. NSLog(@"%@", date);

    结果:

    1. 2015-06-14 13: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

  • 相关阅读:
    Ignatius and the Princess II(全排列)
    中缀式变后缀式
    前缀式计算(前缀表达式)
    Mysql中的锁机制详解
    Mysql关于事务并发带来的问题
    第三方实用API接口汇总
    Mysql 性能优化Explain详解
    Mysql性能分析工具 SHOW PROFILE、 SHOW STATUS
    Mysql慢查询分析
    【数字图像处理】霍夫变换实现
  • 原文地址:https://www.cnblogs.com/tate-zwt/p/4574979.html
Copyright © 2011-2022 走看看