iOS 几个时间类型

1. NSDate

Apple iOS 中的日期类型, 它本身是没有显示的格式的, 在界面上展示的时候需要借助其他类型变量.
常用实例化方法:

  • +date
    获取当前时间的date
  • +dateWithTimeIntervalSinceNow:
    传入一个以秒为单位的时间间隔interval获取到从现在开始interval秒后的一个date
  • +dateWithTimeInterval:sinceDate:
    传入一个以秒为单位的时间间隔interval和一个date获取到从date开始interval秒后的一个date
  • +dateWithTimeIntervalSinceReferenceDate:
    传入一个以秒为单位的时间间隔interval获取到从2001年1月1日0时0分0秒UTC(世界标准时间)开始interval秒后的一个date
  • +dateWithTimeIntervalSince1970:
    传入一个以秒为单位的时间间隔interval获取到从1970年1月1日0时0分0秒UTC(世界标准时间)开始interval秒后的一个date

Apple官方文档说明如下

   + date
Creates and returns a new date set to the current date and time.
   + dateWithTimeIntervalSinceNow:
Creates and returns an NSDate object set to a given number of seconds from the current date and time.
   + dateWithTimeInterval:sinceDate:
Creates and returns an NSDate object set to a given number of seconds from the specified date.
   + dateWithTimeIntervalSinceReferenceDate:
Creates and returns an NSDate object set to a given number of seconds from 00:00:00 UTC on 1 January 2001.
   + dateWithTimeIntervalSince1970:
Creates and returns an NSDate object set to the given number of seconds from 00:00:00 UTC on 1 January 1970. 

2. NSTimeInterval

Apple官方文档说明如下
这里写图片描述
它是用来实例化一个时间间隔, 单位是秒.
数据类型是double
它只可以以秒为单位被实例化, 它拥有10, 000年时间范围内的亚毫秒级的精度.

3. NSDateFormatter

时间显示格式对象
可通过对象方法 -setDateFormat: 设置格式.
例如:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

获取当前时间:

    NSDate *date = [NSDate date];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss"];

    NSString *DateTime = [formatter stringFromDate:date];
    NSLog(@"%@",DateTime);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值