objective-C之NSDate相关类(一)
2016-06-12 17:53
495 查看
一,概述
NSDate是专门处理时间对象的一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate表示一个具体的时间点,可以进行一些常见的日期\时间处理.
二,常用初始化方法
1>+ (instancetype)date
示例
[objc] view
plain copy
NSDate * date =[NSDate date];// 创建时间对象,获得当前时间
注意:date方法返回的是当前时间(now), 但是这个时间是格林尼治时间(GMT),并不是我们传统意义上的北京时间. 格林威治时间属于0时区,北京属于东8区;
比如: 北京时间是上午11:04
打印出来的结果确是
2>- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date
示例
[objc] view
plain copy
NSDate * date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
含义: 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可
注意:NSTimeInterval 是双精度类型,单位是秒.
typedefdouble NSTimeInterval;
三,其它相关方法和属性
typedef double NSTimeInterval;
#define NSTimeIntervalSince1970 978307200.0
1>NSDate类
[objc] view
plain copy
@interface NSDate : NSObject <NSCopying, NSSecureCoding>
@property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
plain copy
@interface NSDate (NSExtendedDate)
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate; //获取两个时间的时间差
@property (readonly) NSTimeInterval timeIntervalSinceNow;
@property (readonly) NSTimeInterval timeIntervalSince1970; // 返回1970-1-1开始走过的毫秒数
- (id)addTimeInterval:(NSTimeInterval)secs;//返回以目前的实例中保存的时间为基准,然后过了secs秒的时间
- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;
- (NSDate *)earlierDate:(NSDate *)anotherDate; // 返回比较早的那个时间
- (NSDate *)laterDate:(NSDate *)anotherDate; // 返回比较晚的那个时间
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
- (BOOL)isEqualToDate:(NSDate *)otherDate;//与otherDate比较,相同返回YES
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
@end
plain copy
@interface NSDate (NSDateCreation)
/*类方法*/
+ (instancetype)date; //当前时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;//相对于当前时间
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti; //相对于2001年1月1日0时0分0秒
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;//相对于1970年1月1日0时0分0秒
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; //相对于已知的某个时间点
+ (NSDate *)distantFuture; //不可达到的未来的某个时间点
+ (NSDate *)distantPast; //不可达到的过去的某个时间点
注意: iOS中用NSDate表示的时间只能在distantPast和distantFuture之间!
/*实例方法*/
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; //相对于当前时间
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs; //相对于1970年1月1日0时0分0秒
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; //相对于已知的某个时间点
@end
[objc] view
plain copy
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
self.timeLabel.text = [dateFormatter stringFromDate:date];
NSString ->NSDate
[objc] view
plain copy
NSString *dateString = self.timeLabel.text;
NSDate *nowDate = [dateFormatter dateFromString:dateString];
self.timeLabel2.text = [dateFormatter stringFromDate:nowDate];
[objc] view
plain copy
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *dateString = @"2016/06/06 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSDateComponents *com = [[NSCalendar currentCalendar]components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:theDate]; NSInteger year = com.year;
NSInteger month = com.month;
NSInteger day = com.day;
NSInteger hour = com.hour;
NSInteger minute = com.minute;
NSInteger second = com.second;
self.timeLabel2.text = [NSString stringWithFormat:@"%ld年%ld月%ld日 %02ld时:%02ld分:%02ld秒, year, month, day, hour, minute, second];
第二种写法:
[objc] view
plain copy
NSString *dateString = @"2016/06/06 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:theDate];
NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
// 2.格式化日期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if ([cmp1 day] == [cmp2 day]) { // 今天
formatter.dateFormat = @"今天 HH:mm";
} else if ([cmp1 year] == [cmp2 year]) { // 今年
formatter.dateFormat = @"MM-dd HH:mm";
} else {
formatter.dateFormat = @"yyyy-MM-dd HH:mm";
}
NSString *time = [formatter stringFromDate:theDate];
// 3.显示时间
self.timeLabel2.text = time;
注意:通过改变NSDateFormatter的dateFormat表现形式可以实现各种你想要的时间表示形式,
比如
[objc] view
plain copy
NSString *dateString = @"2015/09/08 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"'日期:'yyyy有鬼MM
NSDate是专门处理时间对象的一个类簇,我们所使用的NSDate对象,都是NSDate的私有子类的实体. NSDate表示一个具体的时间点,可以进行一些常见的日期\时间处理.
二,常用初始化方法
1>+ (instancetype)date
示例
[objc] view
plain copy
NSDate * date =[NSDate date];// 创建时间对象,获得当前时间
注意:date方法返回的是当前时间(now), 但是这个时间是格林尼治时间(GMT),并不是我们传统意义上的北京时间. 格林威治时间属于0时区,北京属于东8区;
比如: 北京时间是上午11:04
打印出来的结果确是
2>- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date
示例
[objc] view
plain copy
NSDate * date = [[NSDate alloc] initWithTimeInterval:60 sinceDate:[NSDate date]];
含义: 获取从某个日期开始往前或者往后多久的日期,此处60代表60秒,如果需要获取之前的,将60改为-60即可
注意:NSTimeInterval 是双精度类型,单位是秒.
typedefdouble NSTimeInterval;
三,其它相关方法和属性
typedef double NSTimeInterval;
#define NSTimeIntervalSince1970 978307200.0
1>NSDate类
[objc] view
plain copy
@interface NSDate : NSObject <NSCopying, NSSecureCoding>
@property (readonly) NSTimeInterval timeIntervalSinceReferenceDate;
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti NS_DESIGNATED_INITIALIZER;
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder NS_DESIGNATED_INITIALIZER;
@end
2>NSDate分类拓展方法_NSDate (NSExtendedDate)
[objc] viewplain copy
@interface NSDate (NSExtendedDate)
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate; //获取两个时间的时间差
@property (readonly) NSTimeInterval timeIntervalSinceNow;
@property (readonly) NSTimeInterval timeIntervalSince1970; // 返回1970-1-1开始走过的毫秒数
- (id)addTimeInterval:(NSTimeInterval)secs;//返回以目前的实例中保存的时间为基准,然后过了secs秒的时间
- (instancetype)dateByAddingTimeInterval:(NSTimeInterval)ti;
- (NSDate *)earlierDate:(NSDate *)anotherDate; // 返回比较早的那个时间
- (NSDate *)laterDate:(NSDate *)anotherDate; // 返回比较晚的那个时间
- (NSComparisonResult)compare:(NSDate *)other;
该方法用于排序时调用:
. 当实例保存的日期值与anotherDate相同时返回NSOrderedSame
. 当实例保存的日期值晚于anotherDate时返回NSOrderedDescending
. 当实例保存的日期值早于anotherDate时返回NSOrderedAscending
- (BOOL)isEqualToDate:(NSDate *)otherDate;//与otherDate比较,相同返回YES
@property (readonly, copy) NSString *description;
- (NSString *)descriptionWithLocale:(nullable id)locale;
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
@end
3>NSDate分类拓展方法_NSDate (NSDateCreation)
[objc] viewplain copy
@interface NSDate (NSDateCreation)
/*类方法*/
+ (instancetype)date; //当前时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;//相对于当前时间
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti; //相对于2001年1月1日0时0分0秒
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;//相对于1970年1月1日0时0分0秒
+ (instancetype)dateWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; //相对于已知的某个时间点
+ (NSDate *)distantFuture; //不可达到的未来的某个时间点
+ (NSDate *)distantPast; //不可达到的过去的某个时间点
注意: iOS中用NSDate表示的时间只能在distantPast和distantFuture之间!
/*实例方法*/
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs; //相对于当前时间
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs; //相对于1970年1月1日0时0分0秒
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date; //相对于已知的某个时间点
@end
四,相关用法
1>显示当前时间
NSDate ->NSString[objc] view
plain copy
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy/MM/dd hh:mm:ss"];
self.timeLabel.text = [dateFormatter stringFromDate:date];
NSString ->NSDate
[objc] view
plain copy
NSString *dateString = self.timeLabel.text;
NSDate *nowDate = [dateFormatter dateFromString:dateString];
self.timeLabel2.text = [dateFormatter stringFromDate:nowDate];
2>获取指定日期的年,月,日,时,分,秒
第一种写法:[objc] view
plain copy
[dateFormatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *dateString = @"2016/06/06 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSDateComponents *com = [[NSCalendar currentCalendar]components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:theDate]; NSInteger year = com.year;
NSInteger month = com.month;
NSInteger day = com.day;
NSInteger hour = com.hour;
NSInteger minute = com.minute;
NSInteger second = com.second;
self.timeLabel2.text = [NSString stringWithFormat:@"%ld年%ld月%ld日 %02ld时:%02ld分:%02ld秒, year, month, day, hour, minute, second];
第二种写法:
[objc] view
plain copy
NSString *dateString = @"2016/06/06 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSUInteger unitFlags = NSCalendarUnitYear| NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
NSDateComponents *cmp1 = [calendar components:unitFlags fromDate:theDate];
NSDateComponents *cmp2 = [calendar components:unitFlags fromDate:[NSDate date]];
// 2.格式化日期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
if ([cmp1 day] == [cmp2 day]) { // 今天
formatter.dateFormat = @"今天 HH:mm";
} else if ([cmp1 year] == [cmp2 year]) { // 今年
formatter.dateFormat = @"MM-dd HH:mm";
} else {
formatter.dateFormat = @"yyyy-MM-dd HH:mm";
}
NSString *time = [formatter stringFromDate:theDate];
// 3.显示时间
self.timeLabel2.text = time;
注意:通过改变NSDateFormatter的dateFormat表现形式可以实现各种你想要的时间表示形式,
比如
[objc] view
plain copy
NSString *dateString = @"2015/09/08 3:2:34";
NSDate *theDate = [dateFormatter dateFromString:dateString];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"'日期:'yyyy有鬼MM
相关文章推荐
- 网页中通过swfobject插入Flash动画以及设置透明
- YYModel 源码解读(二)之NSObject+YYModel.h (1)
- Cannot use object of type yii\db\Connection as array
- All object files and libraries for bitcode must be generated from...
- caffe_.mexa64': libcaffe.so: cannot open shared object file: No such file or directory
- wish8.6: error while loading shared libraries: libXss.so.1: cannot open shared object file: No such
- 理解 Objective-C Runtime
- objective-c与c字符串互相转换
- Objective-C --- - UITableView 一注册机制简单例子(梳理总结)
- Hiberna中fromObject方法执行的时候,没有成功,前台页面无法获取json对象。
- SelectObject 函数详解
- Objective-C 起源
- commons-pool实战之 PoolableObjectFactory、ObjectPool、 KeyedPoolableObjectFactory......
- java 多线程 Synchronized方法和方法块 synchronized(this)和synchronized(object)的理解
- java.io.EOFException java.io.ObjectInputStream$PeekInputStream.readFully 错误
- js数据类型只string,object
- Qt分析:Qt中的两种定时器(可是QObject为什么要提高定时器呢,没必要啊。。。)
- Android Studio之could not reserve enough space for object heap报错
- 勘误 win7x64下对OpenProcedure的ObjectHook
- Qt在windows 平台操作保存execel的表格(通过QAxObject来操作)