加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

NSDate 日期操作工具类

发布时间:2020-12-16 07:47:50 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 // // main.m // OC05-task-06 // // Created by Xin the Great on 15-1-26. // Copyright (c) 2015年 Xin the Great. All rights reserved. // #impo

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

    //  
    //  main.m  
    //  OC05-task-06  
    //  
    //  Created by Xin the Great on 15-1-26.  
    //  Copyright (c) 2015年 Xin the Great. All rights reserved.  
    //  
      
    #import <Foundation/Foundation.h>  
      
    int main(int argc,const charchar * argv[]) {  
        @autoreleasepool {  
            // insert code here...  
            //////////////////////NSDate --- 日期//////////////////////  
              
            //获取当前系统的时间   标准时间 GMT  格林尼治时间  
            NSDate *date = [NSDate date];  
            NSLog(@"date is %@",date);  
              
            NSDate *date1 = [[NSDate alloc] init];  
            NSLog(@"date1 is %@",date1);  
              
            //获取时间戳   以秒为单位  
            NSTimeInterval time1970 = [date timeIntervalSince1970];  
            NSLog(@"time1970 is %.1f",time1970);  
              
            NSTimeInterval time2001 = [date timeIntervalSinceReferenceDate];  
            NSLog(@"time2001 is %.1f",time2001);  
              
            NSTimeInterval time = [date timeIntervalSinceNow];  
            NSLog(@"time is %.1f",time);  
              
            //获取昨天的时间  
            NSTimeInterval second = 224 * 660 * 60;  
            NSDate *yesterDayDate = [[NSDate alloc] initWithTimeIntervalSinceNow:-second];  
            NSLog(@"yesterDayDate is %@",yesterDayDate);  
              
            //获取明天的时间  
            NSDate *tomorrowDayDate = [NSDate dateWithTimeInterval:second sinceDate:[NSDate date]];  
            NSLog(@"tomorrowDayDate is %@",tomorrowDayDate);  
              
            //获得未来的某一个时间  
            NSDate *future = [NSDate distantFuture];  
            NSLog(@"future is %@",future);  
              
            //获得古代的某一个时间  
            NSDate *past = [NSDate distantPast];  
            NSLog(@"past is %@",past);  
              
            //日期的比较  
    //        BOOL isTure = [date isEqualToDate:date1];  
    //        NSLog(@"isTure is %d",isTure);  
              
            //返回两个时间比较早的那个时间  
            NSDate *earlierDate = [tomorrowDayDate earlierDate:future];  
            NSLog(@"earlierDate is %@",earlierDate);  
              
            //返回两个时间比较晚的那个时间  
            NSDate *later = [tomorrowDayDate laterDate:future];  
            NSLog(@"later is %@",later);  
              
              
            //将时间戳转换成字符串  
            NSString *str = @"123456789";  
            NSTimeInterval second2 = [str doubleValue];  
            NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:second2];  
            NSLog(@"date3 is %@",date3);  
              
            //格式化日期类  
            NSDateFormatter *df = [[NSDateFormatter alloc] init];  
            [df setDateFormat:@"yyyy年MM月dd日 HH小时mm分钟ss秒 ZZZZ"];  
              
            //将日期按照格式化日期类转换为字符串  
            NSString *str2 = [df stringFromDate:date3];  
            NSLog(@"str2 is %@",str2);  
              
            //通过字符串转换为date  
            NSDate *date4 = [df dateFromString:str2];  
            NSLog(@"date4 is %@",date4);  
              
        }  
        return 0;  
    }  

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读