iOS NSDate

来源:互联网 发布:陕西干部网络培训网 编辑:程序博客网 时间:2024/04/30 21:16

前言

NSDate

@interface NSDate : NSObject <NSCopying, NSSecureCoding>

NSDate 用来表示公历的 GMT 时间(格林威治时间)。是独立与任何历法的,它只是时间相对于某个时间点的时间差;NSDate 是进行日历计算的基础。
NSDateComponents

@interface NSDateComponents : NSObject <NSCopying, NSSecureCoding>

NSDateComponents 封装了具体年月日、时秒分、周、季度等。将时间表示成适合人类阅读和使用的方式,通过 NSDateComponents 可以快速而简单地获取某个时间点对应的“年”,“月”,“日”,“时”,“分”,“秒”,“周”等信息。当然一旦涉及了年月日时分秒就要和某个历法绑定,因此 NSDateComponents 必须和 NSCalendar 一起使用,默认为公历。NSDateComponents 除了像上面说的表示一个时间点外,还可以表示时间段,例如:两周,三个月,20年,7天,10分钟,50秒等等。时间段用于日历的计算,例如:获取当前历法下,三个月前的某个时间点。可以说,要获取某个时间点在某个历法下的表示,需要 NSDateComponents; 要计算当前时间点在某个历法下对应的一个时间段前或后的时间点,需要 NSDateComponents。NSDateComponents 返回的 day, week, weekday, month, year 这一类数据都是从 1 开始的。因为日历是给人看的,不是给计算机看的,从 0 开始就是个错误。
NSDateFormatter

@interface NSDateFormatter : NSFormatter

NSDateFomatter 表示的时间默认以公历(即阳历)为参考,可以通过设置 calendar 属性变量获得特定历法下的时间表示。
回到顶部
1、NSDate 的创建

// 对象方法    // 当前时间值,GMT 时间    NSDate *date1 = [[NSDate alloc] init];    // 当前时间加 n 秒后的值    NSDate *date2 = [[NSDate alloc] initWithTimeIntervalSinceNow:10];    // 某个时间增加 n 秒后的值    NSDate *date3 = [[NSDate alloc] initWithTimeInterval:10 sinceDate:date2];    // 从 1970-01-01 00:00:00 (GMT) 增加 n 秒后的值    NSDate *date4 = [[NSDate alloc] initWithTimeIntervalSince1970:3600];    // 从 2001-01-01 00:00:00 (GMT) 增加 n 秒后的值    NSDate *date5 = [[NSDate alloc] initWithTimeIntervalSinceReferenceDate:10];// 类方法    // 当前时间值,GMT 时间    NSDate *date6 = [NSDate date];    // 当前时间加 n 秒后的值    NSDate *date7 = [NSDate dateWithTimeIntervalSinceNow:10];    // 某个时间增加 n 秒后的值    NSDate *date8 = [NSDate dateWithTimeInterval:10 sinceDate:date2];    // 从 1970-01-01 00:00:00 (GMT) 增加 n 秒后的值    NSDate *date9 = [NSDate dateWithTimeIntervalSince1970:3600];    // 从 2001-01-01 00:00:00 (GMT) 增加 n 秒后的值    NSDate *date10 = [NSDate dateWithTimeIntervalSinceReferenceDate:10];    // 未来某一个随机时间    NSDate *date11 = [NSDate distantFuture];    // 过去某一个随机时间    NSDate *date12 = [NSDate distantPast];    // 指定时间间隔    /*        某个时间增加 n 秒后的值    */    NSDate *date13 = [date6 dateByAddingTimeInterval:20];

回到顶部
2、NSDate 时间间隔的计算

NSDate *date1 = [NSDate date];NSDate *date2 = [date1 dateByAddingTimeInterval:20];// 实例保存的时间 与 当前时间 的时间间隔,单位 秒NSTimeInterval interval1 = [date2 timeIntervalSinceNow];// 实例保存的时间 与 指定时间 的时间间隔,单位 秒NSTimeInterval interval2 = [date2 timeIntervalSinceDate:date1];// 实例保存的时间 与 1970-01-01 00:00:00 (GMT) 的时间间隔,单位 秒NSTimeInterval interval3 = [date1 timeIntervalSince1970];// 实例保存的时间 与 2001-01-01 00:00:00 (GMT) 的时间间隔,单位 秒NSTimeInterval interval4 = [date1 timeIntervalSinceReferenceDate];// 当前时间 与 2001-01-01 00:00:00 (GMT) 的时间间隔,单位 秒NSTimeInterval interval5 = [NSDate timeIntervalSinceReferenceDate];

回到顶部
3、NSDate 时间的比较

NSDate *date1 = [NSDate date];NSDate *date2 = [date1 dateByAddingTimeInterval:10];// isEqualToDate    // 判断两个时间是否相等    BOOL bl = [date1 isEqualToDate:date2];// compare    // 比较两个时间大小    NSComparisonResult result = [date1 compare:date2];// earlierDate    // 比较两个时间,返回 较早的时间    NSDate *date3 = [date1 earlierDate:date2];// laterDate    // 比较两个时间,返回 较晚的时间    NSDate *date4 = [date1 laterDate:date2];

回到顶部
4、NSDateComponents 的创建

// 由手动设置创建NSDateComponents *compt1 = [[NSDateComponents alloc] init];// 日历[compt1 setCalendar:[NSCalendar currentCalendar]];// 时区[compt1 setTimeZone:[NSTimeZone systemTimeZone]];// 纪元[compt1 setEra:1];// 年[compt1 setYear:2016];// 月[compt1 setMonth:3];// 日[compt1 setDay:1];// 时[compt1 setHour:00];// 分[compt1 setMinute:10];// 秒[compt1 setSecond:55];// 微妙[compt1 setNanosecond:280];// 刻钟[compt1 setQuarter:0];// 周几[compt1 setWeekday:3];// 指定日期为本月的第几个星期几[compt1 setWeekdayOrdinal:1];// 指定日期为本月的第几周[compt1 setWeekOfMonth:1];// 指定日期为本年的第几周[compt1 setWeekOfYear:10];//[compt1 setYearForWeekOfYear:2016];//  由已知 date 创建/*    只有明确指定了 unitFlags,NSDateComponents 相应的那一部分才有值*/NSDateComponents *compt2 = [[NSCalendar currentCalendar] components:NSCalendarUnitEra                                                                  | NSCalendarUnitYear                                                                  | NSCalendarUnitMonth                                                                  | NSCalendarUnitDay                                                                  | NSCalendarUnitHour                                                                  | NSCalendarUnitMinute                                                                  | NSCalendarUnitSecond                                                                  | NSCalendarUnitNanosecond                                                                  | NSCalendarUnitQuarter                                                                  | NSCalendarUnitWeekday                                                                  | NSCalendarUnitWeekdayOrdinal                                                                  | NSCalendarUnitWeekOfMonth                                                                  | NSCalendarUnitWeekOfYear                                                                  | NSCalendarUnitYearForWeekOfYear                                                                  | NSCalendarUnitCalendar                                                                  | NSCalendarUnitTimeZone                                                           fromDate:[NSDate date]];

回到顶部
5、NSDateComponents 时间间隔的计算

NSDate *date1 = [NSDate date];NSDate *date2 = [NSDate dateWithTimeInterval:5*60*60+75 sinceDate:date1];NSDateComponents *compt = [[NSCalendar currentCalendar] components:NSCalendarUnitMinute                                                                  | NSCalendarUnitSecond                                                           fromDate:date1                                                             toDate:date2                                                            options:0];

回到顶部
6、NSDateComponents 与 NSDate 的相互转换

NSCalendar *calendar = [NSCalendar currentCalendar];// NSDate 转 NSDateComponents    NSDateComponents *compt = [calendar components:NSCalendarUnitYear                                                  | NSCalendarUnitMonth                                                  | NSCalendarUnitDay                                           fromDate:[NSDate date]];// NSDateComponents 转 NSDate    // 转换时间时,使用默认的时区 GMT 时区    NSDate *date = [calendar dateFromComponents:compt];                                                     // 得到本地时间,避免时区问题    date = [date dateByAddingTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMTForDate:date]];    

回到顶部
7、NSDateComponents 与 NSDate 的计算

NSDateComponents *compt = [[NSDateComponents alloc] init];[compt setDay:25];[compt setHour:4];[compt setMinute:66];// NSDate 加上 NSDateComponents 表示的一段时间NSDate *date = [[NSCalendar currentCalendar] dateByAddingComponents:compt toDate:[NSDate date] options:0];// 得到本地时间,避免时区问题date = [date dateByAddingTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMTForDate:date]];    

回到顶部
8、NSDateFormatter 的时间格式化

G -- 纪元                  一般会显示公元前(BC)和公元(AD)y -- 年                    假如是2013年,那么 yyyy=2013,yy=13M -- 月                    假如是3月,那么 M=3,MM=03,MMM=Mar,MMMM=March;假如是11月,那么M=11,MM=11,                            MMM=Nov,MMMM=Novemberw -- 一年中的第几周         假如是1月8日,那么 w=2(这一年的第二个周)W -- 一个月中的第几周       与日历排列有关,假如是2013年4月21日,那么 W=4(这个月的第四个周)F -- 月份包含的周           与日历排列无关,和上面的 W 不一样,F 只是单纯以7天为一个单位来统计周,例如7号一定是第一个周,                           15号一定是第三个周,与日历排列无关。D -- 一年中的第几天         假如是1月20日,那么 D=20(这一年的第20天);假如是2月25日,那么 D=31+25=56(这一年的第56天)d -- 一个月中的第几天       假如是5号,那么 d=5,dd=05   假如是15号,那么 d=15,dd=15E -- 星期几                假如是星期五,那么 E=Fri,EEEE=Fridaya -- 上午(AM)/下午(PM)H -- 24小时制              显示为0--23,假如是午夜00:40,那么 H=0:40,HH=00:40h -- 12小时制              显示为1--12,假如是午夜00:40,那么 h=12:40K -- 12小时制              显示为0--11,假如是午夜00:40,那么 K=0:40,KK=00:40k -- 24小时制              显示为1--24,假如是午夜00:40,那么 k=24:40m -- 分钟                  假如是5分钟,那么 m=5,mm=05;假如是45分钟,那么 m=45,mm=45s -- 秒                    假如是5秒钟,那么 s=5,ss=05;假如是45秒钟,那么 s=45,ss=45S -- 毫秒                  一般用 SSS 来显示z -- 时区                  表现形式为 GMT+08:00Z -- 时区                  表现形式为 +0800// 使用 NSDateFormatter 转换时间字符串时,默认的时区是系统时区,例如在中国一般都是北京时间(+8),// 如果直接转换会导致结果相差8小时,所以一般的做法是先指定时区为GMT标准时间再转换。NSDateFormatter *formatter = [[NSDateFormatter alloc] init];// 设置时区,不设置时默认的时区是系统时区(GMT+8)[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];// 设置日期格式,以字符串表示的日期形式的格式[formatter setDateFormat:@"G yyyy-MM-dd E D F w W a z HH:mm:ss.SSS"];// 格式化日期,GMT 时间,NSDate 转 NSStringNSString *str = [formatter stringFromDate:[NSDate date]];                       

回到顶部
9、1437494603 (秒) 格式 转 YYYY-MM-dd HH:mm:ss 格式

// 计算日期NSDate *date = [NSDate dateWithTimeIntervalSince1970:1437494603];               // 创建时间戳NSDateFormatter *formatter = [[NSDateFormatter alloc] init];                    // 设置日期格式,以字符串表示的日期形式的格式[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];                               // 转换成指定的格式NSString *string = [formatter stringFromDate:date];                             

回到顶部
10、NSDate 与 NSString 的相互转换

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];// 设置时区 GMT[formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];// 加入一些别的英文字符串时,需用单引号来引入[formatter setDateFormat:@"yyyy年MM月dd日 HH点mm分ss秒 'iOS Date Test'"];// NSString 转 NSDateNSDate *date = [formatter dateFromString:@"2016年01月12日 1点8分50秒"];// NSDate 转 NSStringNSString *str = [formatter stringFromDate:date];

回到顶部
11、时区差值转换

// 得到当前时间(世界标准时间 UTC/GMT)NSDate *date = [NSDate date];// 设置系统时区为本地时区NSTimeZone *zone = [NSTimeZone systemTimeZone];// 计算本地时区与 GMT 时区的时间差NSInteger interval = [zone secondsFromGMT];// 在 GMT 时间基础上追加时间差值,得到本地时间date = [date dateByAddingTimeInterval:interval];
0 0
原创粉丝点击