NSDate日期的类

来源:互联网 发布:图像识别 python 编辑:程序博客网 时间:2024/05/16 13:06

一. //NSDate是一个日期的类

    //1.date:无论在哪个时区,date获取的时间是相对应的零时区时间

   NSDate *date=[NSDatedate];

   NSLog(@"%@",date);


    //2.先获取当前所在的时区

    NSTimeZone *zone=[NSTimeZonesystemTimeZone];

   NSLog(@"%@",zone);

    

    //3.获取一下和0时区相差的秒数

   NSInteger seconds=[zone secondsFromGMTForDate:date];

   NSLog(@"%ld",seconds);

    

    //4.通过相差的秒数,能获取到现在的时间

   NSDate *localDate=[NSDatedateWithTimeIntervalSinceNow:seconds];

   NSLog(@"%@",localDate);

    

    NSDate *times=[NSDatedateWithTimeIntervalSince1970:0];

   NSLog(@"%@",times);  1970-01-01 00:00:00 +0000

    

    //5.找到明天的这个时间

   NSDate *tomorrowTime=[NSDatedateWithTimeIntervalSinceNow:(seconds+24*3600)];

   NSLog(@"%@",tomorrowTime);

    

    //6.找到昨天的这个时间

    NSDate *yesterdayTime=[NSDatedateWithTimeIntervalSinceNow:(seconds-24*3600)];

   NSLog(@"%@",yesterdayTime);

   

    //7.时间间隔,对应的double类型

    //计算两个时间对象的时间间隔

   NSTimeInterval interval=[tomorrowTime timeIntervalSinceDate:date];

   NSLog(@"%g",interval);

 

    //8.计算当前时间和固定时间的差值

   NSDate *constTime=[NSDatedateWithTimeIntervalSinceNow:(seconds-7 *3600)];

   NSTimeInterval intervalTime=[constTime timeIntervalSinceDate:date];

   if (intervalTime < 60) {

       NSLog(@"刚刚");

    }elseif (intervalTime >=60 && intervalTime<=3600){

       NSLog(@"%ld分钟前",(NSInteger)intervalTime/60);

    }elseif (intervalTime > 3600 && intervalTime<3600*24){

       NSLog(@"%ld小时前",(NSInteger)intervalTime/3600);

    }

  打印结果:1小时前


0 0
原创粉丝点击