iOS时间与日期处理

NSDate:日期相关的类

NSTimeInterval:时间间隔相关的类

NSDateFormatter:时间格式器相关的类

   学习本节要掌握的知识点:1.日期转时间戳

                                      2.日期转字符串 

                                      3.日期转指定格式的字符串(获得星期几,获得年月日格式显示等等)

                                      4.时间戳转日期

                                      5.字符串转日期

                                      6.指定格式字符串转日期

                                      7.时间的推算

#pragma mark ======NSDate:日期相关的类 GTM(国际标准时间)==========

- (void)viewDidLoad{

      [super viewDidLoad];

//初始化,和一般的数据初始化一样(获取国际的标准时间,和我们服务器的时间相差8个时区)

     NSDate *date = [NSDate date];

    NSLog(@"%@",date);

//打印结果如下:

2016-01-06 14:34:24.964 时间与日期处理[3775:444683] 2016-01-06 06:34:24 +0000

//另一种初始化方式

    /*

     NSTimeInterval: 时间间隔 单位是秒

     

     */

//   这行代码的意思是从现在开始过了多少秒, 过去的时间(-)未来的时间(+)

//  获取当前的日期和时间

    NSTimeInterval interval = 60*60*8;

    NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:(interval)];

    NSLog(@"%@",date1);

// 获取10天以前的日期和时间

    NSTimeInterval interval1 = interval-60*60*24*10;

    NSDate *date2 = [NSDate dateWithTimeIntervalSinceNow:interval1];

    NSLog(@"%@",date2);

    

#pragma mark=======时间戳============

// 时间戳:就是一个时间的标志,是从1970年到现在的一个时间间隔(或者说是字符串)

//       它可以表示一个唯一的时间标识

// 从1970开始计算

 NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:1452044054];//获得一个时间戳 所标示的日期

    NSLog(@"%@",date3);

    

#pragma mark=====日期怎么转换成时间间隔=======

//日期转换成时间间隔——>1.可以获得时间戳(指的是从1970——现在)

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

// 1⃣️. 获得两个日期之间的时间间隔

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

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

    NSTimeInterval timeInterval =fabs([date3 timeIntervalSinceDate:[NSDate date]]);

    NSLog(@"%f",timeInterval);

    

//计算两个时间间隔相差多少小时多少分多少秒

    int h = timeInterval/(60*60);

    NSLog(@"两个日期相差%d小时",h);

    int s = ((int)timeInterval)%(60*60)/60;

    NSLog(@"相隔%d分",s );

    int m = ((int)timeInterval)%(60*60)%60;

    NSLog(@"相隔%d秒",m);

  /*

   取绝对值的方法:不区分 正负数 (无符号)

   abs(int)

   fabs(double)

   fabsf(float)

   

   */

#pragma mark=======日期转时间戳===========

// 2⃣️. 从现在到1970年的时间间隔

// 日期转时间戳

    NSDate *curDate = [NSDate date];

    

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

    

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

    NSLog(@"%@",string);

    

#pragma mark==========两个日期之间的比较===========

//比较1451047216 和 1451847216

//1.将这两个时间戳转换成日期

NSDate *date4 = [NSDate dateWithTimeIntervalSince1970:1451047216];

NSDate *date5 = [NSDate dateWithTimeIntervalSince1970:1451847216];

//2.开始比较

// 比较date4是不是比date5早——>会返回一个比较早的日期

  NSDate *date6 = [date4 earlierDate:date5];

    NSLog(@"比较早的日期%@",date6);

//比较两个日期谁比谁晚

    NSDate *date7 = [date4 laterDate:date5];

    NSLog(@"%@",date7);

//  比较两个日期 是不是相同 ——>返回值BOOL类型

    BOOL result = [date4 isEqualToDate:date5];

    NSLog(@"%d",result);

//=========日期格式器==========

//    1⃣️初始化

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

//  ******格式的属性*********

  /*  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

*/

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

    

    

    

//    ⚠️大写的M表示月,小写的m表示分,它们所代表的意义不一样

//    ⚠️大写的HH 表示24小时制的

//    ⚠️小写的hh 表示12小时制的

//    ⚠️大写的SS 表示 毫秒

//    ⚠️小写的ss 表示 秒

    

//把日期转换成字符串

//    1.必须有日期

    NSDate *now = [NSDate date];

    

//    2.使用日期转换器进行转换

    

    NSString *dateSting = [formatter stringFromDate:now];

    NSLog(@"%@",dateSting);

    

// ==========把字符串转换成日期============

    NSDate *returnDate = [formatter dateFromString:dateSting];

    NSLog(@"%@",returnDate);

    

// ===========把时间戳转换成想要的格式========

//  把1451847216 时间戳转换成 某年某月某日 某周几 上下午

//  把时间戳转换成日期

    NSString *timeStemp = @"1451847216";

    NSDate *date8 = [NSDate dateWithTimeIntervalSince1970:[timeStemp doubleValue]];

    

    NSLog(@"%@",date8);

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

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

//  某年某月某日 某周几 上下午   日期格式器的格式

    Formatter.dateFormat = @"yyyy年MM月dd日 E a";

    

// 使用格式器开始转换

    NSString *returnString = [Formatter stringFromDate:date8];

    

    NSLog(@"%@",returnString);

    

//=============周一转换成星期一=============

    

    

    

}

/*

 作业:在昨天的基础之上,添加一个创建日期的字段

 要求:日期要用时间戳 的形式 存储

 在添加内容的时候 自动保存到 数据库

 */

/*

 1、NSDate 初始化

 NSDate *date = [NSDate date];//当前时间(国际的标准时间)

 NSDate *date1 = [NSDate dateWithTimeIntervalSinceNow:interval];//获得一个时间间隔 到现在的日期(用于推算 前面 或 后面的时间) 过去的时间间隔(-) 将来的时间间隔(+)

 NSDate *date3 = [NSDate dateWithTimeIntervalSince1970:1452044015];//获得 一个时间戳 所表示的日期

 2、NSDate -> 时间戳 (1970-new) -> 字符串表示

 NSDate有一个属性timeIntervalSince1970 -> 可以获得到 (1970 - new) 一个时间间隔 ->转成字符串就是表示日期的时间戳

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

 3、两个日期之间的比较

 - (NSDate *)earlierDate:(NSDate *)anotherDate;//比较两个日期 谁早 返回早的日期

 - (NSDate *)laterDate:(NSDate *)anotherDate;//比较两个日期 谁晚 返回晚的日期

 - (BOOL)isEqualToDate:(NSDate *)otherDate;//比较两个日期是否相同 返回BOOL

 */

/*

 NSDateFormatter 把日期 转换成 需要的格式

 格式化日期的 格式 用字符串 表示

 @"yyyy-MM-dd HH:mm:ss"

 @"2016-01-06 11:06:30";

 作用:

 1、日期 转换成 字符串(指定格式)

 2、字符串(指定格式) 转换成 日期

 *****会把GTM 转换成 系统时间

 1、初始化

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

 2、设置日期格式器的 格式 -> 用字符串表示

 dateFormatter.dateFormat = @"yyyy年MM月dd日 E a";

 3、使用格式器 转换

 (1)把日期 转成 字符串

 NSString *dateS = [dateFormatter stringFromDate:date20];

 (2)把字符串 转换成 日期

 NSDate *date10 = [formatter dateFromString:dateString];

 */

 

原文地址:https://www.cnblogs.com/penglisu/p/5105559.html