iOS OC NSDate基本用法

    xiaoxiao2025-10-22  11

     @autoreleasepool {

      //返回当前时间,以GMT为准

     NSDate * date = [NSDate date];

     NSLog(@"%@", date);

      //显示当前时间距离1970-01-01 000000的秒数

     NSLog(@"%.2f",date.timeIntervalSince1970);

      //从现在起3600秒时候的时间

     NSLog(@"%@", [NSDatedateWithTimeIntervalSinceNow:3600]);

      //1970-01-01 0000003600秒的时间

     NSLog(@"%@", [NSDatedateWithTimeIntervalSince1970:-3600]);

      //返回一个很久之后的时间

     NSLog(@"%@", [NSDatedistantFuture]);

      //返回一个很久之前的时间

     NSLog(@"%@", [NSDatedistantPast]);

      //返回当前系统时区

     NSLog(@"%@", [NSTimeZonesystemTimeZone]);

      //时间戳  格式化时间

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

      dateFormatter.dateFormat = @"G yyyy-MM-dd E D F w W a zhh:mm:ss.SSS";

      NSString * dateStr = [dateFormatterstringFromDate:date];

      NSLog(@"%@",dateStr);

      /*

       G 年代标志符

       y 年

       M 月

       d 日

       h 时在上午或下午 (1~12)

       H 时在一天中 (0~23)

       m 分

       s 秒

       S 毫秒

       E 星期

       D 一年中的第几天

       F 一月中第几个星期几

       w 一年中第几个星期

       W 一月中第几个星期

       a 上午 / 下午 标记符

       k 时在一天中 (1~24)

       K 时在上午或下午 (0~11)

       z 时区

       */

    }

    用于创建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/3116:00:00)

    + (id)distantPast;

    返回很多年以前的某一天。(比如你需要一个比现在(Now)早(小)大很长时间的时间值,则可以调用该方法。测试返回了公元前0001/12/3117: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/01GMT的时间间隔

    -(NSTimeInterval)timeIntervalSinceReferenceDate;

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

    +(NSTimeInterval)timeIntervalSinceReferenceDate;

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

     

     

    4. 将时间表示成字符串

    - (NSString *)description;

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

     

     

     

    NSDate转String

    //获取系统当前时间

    NSDate*currentDate=[NSDatedate];

    //用于格式化NSDate对象

    NSDateFormatter*dateFormatter=[[NSDateFormatteralloc]init];

    //设置格式:zzz表示时区

    [dateFormattersetDateFormat:@"yyyy-MM-ddHH:mm:sszzz"];

    //NSDateNSString

    NSString*currentDateString=[dateFormatterstringFromDate:currentDate];

    //输出currentDateString

    NSLog(@"%@",currentDateString);

    NSString转NSDate

    //需要转换的字符串

    NSString*dateString=@"2015-06-2608:08:08";

    //设置转换格式

    NSDateFormatter*formatter=[[NSDateFormatteralloc]init];

    [formattersetDateFormat:@"yyyy-MM-ddHH:mm:ss"];

    //NSStringNSDate

    NSDate*date=[formatterdateFromString:dateString];
    转载请注明原文地址: https://ju.6miu.com/read-1303406.html
    最新回复(0)