日期类的常用处理

作者: 见哥哥长高了 | 来源:发表于2016-11-26 18:49 被阅读121次
时间戳

时间戳的概念:某一个日期到1970年的秒数大小 成为该日期的时间戳。

NSDate

NSDate的基本概念:在Foundation框架中,提供了NSDate类,它是cocoa提供给用户处理日期的类。他提供日期的创建,比较,计算时间间隔等功能。

创建时间对象

NSDate类为我们提供了很多种创建NSDate的方法 包括类方法个实例化F方法,例如:
****类方法创建NSDate实例****

+ (instancetype)date; //当前时间 
//表示距离此刻过了多少秒的时间点

+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
//表示距离此刻过了secs秒的时间点

+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
//2001/01/01 GMT为基准,然后过了secs秒的时间

 + (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
//以1970/01/01 GMT为基准,然后过了secs秒的时间

 + (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
//以date为基准,然后过了secsToBeAdded秒的时间


****初始化NSDate实例的方法****

- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
//表示距离此刻过了多少秒的时间点

- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
//以1970/01/01 GMT为基准,然后过了secs秒的时间

- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
//以date为基准,然后过了secsToBeAdded秒的时间

- (instancetype)init NS_DESIGNATED_INITIALIZER;
//当前时间

- (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti NS_DESIGNATED_INITIALIZER;
//2001/01/01 GMT为基准,然后过了secs秒的时间

****实例化方法创建NSDate实例****

- (id)addTimeInterval:(NSTimeInterval)seconds NS_DEPRECATED(10_0, 10_6, 2_0, 4_0);
//以目前的实例中保存的时间为基准,然后过了secs秒的时间

- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti NS_AVAILABLE(10_6, 2_0);
//以目前的实例中保存的时间为基准,然后过了secs秒的时间

****NSDate实例比较的方法****

- (NSDate *)earlierDate:(NSDate *)anotherDate;
//与anotherDate比较,返回较早的那个日期

- (NSDate *)laterDate:(NSDate *)anotherDate;
//与anotherDate比较,返回较晚的那个日期

- (NSComparisonResult)compare:(NSDate *)other;
//返回的结果是:
 typedef NS_ENUM(NSInteger, NSComparisonResult) {NSOrderedAscending = -1L, NSOrderedSame, NSOrderedDescending};

       NSComparisonResult comparisonResult = [date1 compare:date2];
        if (comparisonResult == NSOrderedAscending) {
            //第一个小于第二个
        }else if (comparisonResult == NSOrderedDescending){
            //第一个大于第二个
        }else if (comparisonResult == NSOrderedSame){
            //第一个等于第二个
        }

- (BOOL)isEqualToDate:(NSDate *)otherDate;
//与anotherDate比较,返回是否相等结果bool

此外在时间的比较上我们可以根据时间戳的大小来进行

              //日期的比较 时间戳值的比较
        if ([date1 timeIntervalSince1970] > [date2 timeIntervalSince1970]) {
            NSLog(@"date1 > date2");
        }else if ([date1 timeIntervalSince1970] < [date2 timeIntervalSince1970]){
            NSLog(@"date1 > date2");
        }else{
            NSLog(@"date1 = date2");
        } 

****日期格式化****
日期格式化:日期对象---->字符串对象
日期格式化有其固定的方式:

        //创建日期对象
 NSDate *date =[NSDate date];

        //创建日期格式化
 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];

        //设置日期的格式
 [dateFormatter setDateFormat:@"yyyy年MM月dd日 HH时mm分ss秒"];

        //向字符串的转化
 NSString *dateString = [dateFormatter stringFromDate:date];
        NSLog(@"格式化后的:%@",dateString);

日期格式化:字符串对象---->日期对象

NSString *string = @"2016-11-27 17:24:05";

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

[dateFormatter1 setDateFormat:@"yyyy-M-d HH:mm:ss"];

NSDate *date1 = [dateFormatter1 dateFromString:string];

NSLog(@"%@",date1);

我们所在时区的不同,我们获取到的日期对象 或者根绝日期对象获得的字符串对象也是不一样的:

获取到所有时区的名字:

        NSArray *arr = [NSTimeZone knownTimeZoneNames];
        NSLog(@"%@",arr);

打印数组 我们可以获取所有的时区的名字,如下:



根据不同的时区名字 我们可以创建时区对象 今儿获取到不同时区所对应的日期/时间对象

NSTimeZone *timeZone = [[NSTimeZone alloc]initWithName:@"Asia/Qyzylorda"];

[dateFormatter setTimeZone:timeZone];  
    
NSString *dateString1 = [dateFormatter stringFromDate:date];

NSLog(@"格式化后的:%@",dateString1);

此外 在格式的互相转化过程中,以下是一些类似于代表年月日的字母信息所表示的意义:


****其他用法****

@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
以YYYY-MM-DD HH:MM:SS ±HHMM的格式表示时间。
//将NSDate实例转化成字符串

//date对象------>时间戳值
NSTimeInterval timeInterval = [date timeIntervalSince1970];

  //时间戳值------>NSDate对象
 NSDate *date4 = [NSDate dateWithTimeIntervalSince1970:3600 * 24];
 NSLog(@"%@",date4); // 1970-01-02 00:00:00 +0000

相关文章

  • 聊聊java的日期处理类

    java里面常用的日期处理类: java里面常用的日期处理类主要有: Date Calendar TimeZone...

  • 日期类的常用处理

    时间戳 时间戳的概念:某一个日期到1970年的秒数大小 成为该日期的时间戳。 NSDate NSDate的基本概念...

  • 一文搞懂python datetime模块-日期时间处理

    前言 datetime是python的内置模块,用来处理日期和时间。该模块常用的类有: 类名功能说明date日期对...

  • java时间日期的处理

    本文摘录自:Java时间日期的处理:Java Date类、Calendar类详解深入理解Java常用类-----时...

  • 常用的日期处理

    在项目开发中经常会碰到需要对日期一些操作、格式化之类的,下面列了几个常用的日期处理 之前项目需要展示某些日期对应周...

  • Java 时间处理类 Calendar

    Date 类中有很多方法都过期了,推荐使用Calenddar操作日期。下面列出了常用的时间处理例子

  • 时间与日期

    在iOS原生类中用以处理时间与日期的类:NSCalendar 日历处理NSDateComponents 将日期进...

  • 【手把手教你】Python处理金融数据

    时间序列分析之日期处理 datetime处理日期 python常用的处理时间的库有:datetime,time,c...

  • 2020-06-27【日期类Date】

    Date常用方法 SimpleDateFormat 日期工具类 日历类Calender

  • Java日期处理类

    导语 最重要的就是使用SimpleDateFormat类进行日期格式的转换。 主要内容 Date类的使用 Simp...

网友评论

    本文标题:日期类的常用处理

    本文链接:https://www.haomeiwen.com/subject/izrgpttx.html