iOS中时间日期的基本应用

 

 * NSDate 日期

 * NSTimeInterval 时间间隔

 * NSDateFormatter 时间格式器

时间的推算

 * NSDate 日期相关的类 GTM(国际标准时间)

 时间戳:从1970到现在 的时间间隔(字符串)可以表示一个唯一的时间标识

三种当前时间的初始化:

  //1、当前时间

    NSDate *date = [NSDatedate];

    NSLog(@"%@",date);


// 2、 (NSTimeInterval)是一个时间间隔单位是秒

    //从现在开始过了多少秒过去的时间是负数 未来的时间是正数

    NSTimeInterval interval = 60*60*8;

    NSDate *date1 = [NSDatedateWithTimeIntervalSinceNow:interval];

     NSLog(@"%@",date1);


//3、获得时间戳所表示的日期

    NSDate *date3 = [NSDatedateWithTimeIntervalSince1970:1452044015];

    NSLog(@"date1:%@",date3);

日期怎么转换成时间间隔

1、可以获得时间间隔(1970-现在)

2、可以获得两个日期之间的时间间隔

// - (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;

    

    //1452044015这个时间戳对应的日期与当前时间的 时间间隔

    

    NSTimeInterval timeInterVal = fabs([date3 timeIntervalSinceDate:[NSDatedate]]);

    NSLog(@"%f",timeInterVal);

//计算两个日期 差多少小时多少分 多少秒

    int h = timeInterVal/(60*60);

    int remainTimeInterVal =((int)timeInterVal)%(60*60);

    NSLog(@"%d",h);

    int m = remainTimeInterVal/60;

        NSLog(@"%d",m);

    int s =remainTimeInterVal%60;

    NSLog(@"%d",s);


***日期 时间戳

 

   NSDate *curDate = [NSDatedate];

    NSLog(@"%f",curDate.timeIntervalSince1970);

    NSString *timeStamp = [NSStringstringWithFormat:@"%d",(int)curDate.timeIntervalSince1970];

     NSLog(@"%@",timeStamp);


#pragma mark***********日期时间格式*********************

    //格式器初始化

    NSDateFormatter *formatter = [[NSDateFormatteralloc]init];

    //格式的属性

    formatter.dateFormat =@"yyyy-MM-dd HH:mm:ss";

//1、把日期转换成字符串

    //1)日期

    NSDate *now = [NSDatedate];

    

//2、使用日期格式器进行转换

    NSString *dateString  = [formatter stringFromDate:now];

    NSLog(@"%@",dateString);

    

 //3、把字符串转换成日期

    NSDate *date10 = [formatter dateFromString:dateString];

    NSLog(@"%@",date10);

    

    //1451847216

 

    //1、找到时间戳所在日期

    NSDate *newDate = [NSDatedateWithTimeIntervalSince1970:(1451847216)];

    //2、初始化日期格式器 并指定格式

    NSDateFormatter *ddateFormatter = [[NSDateFormatteralloc]init];

    //  某年某月某日某周几 上下午

    ddateFormatter.dateFormat = @"yyyyMMdd E a";

   //3、使用格式器开始转换

    NSString *newString = [ddateFormatter stringFromDate:newDate];

    NSLog(@"%@",newString);



日期格式如下:

 y 

 M  年中的月份

 D  当天是今年的第多少天

 d  月份中的天数

 F  月份中的周数

 E  星期几

 a  Am/pm

 H  一天中的小时数(0-23

 k  一天中的小时数(1-24

 K  am/pm 中的小时数(0-11  Number  0

 h  am/pm 中的小时数(1-12  Number  12

 m  小时中的分钟数  Number  30

 s  分钟中的秒数  Number  55

 S  毫秒数  Number  978

 z  时区  General time zone  Pacific Standard Time; PST; GMT-08:00

 Z  时区  RFC 822 time zone  -0800





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值