NSObject--NSDate常用方法
2015-11-03 16:17
375 查看
NSDate *date=[NSDate
date];//获取当前时间
NSLog(@"%@",date);//2015-11-03 03:15:10 +0000
NSDate *date1=[NSDate
dateWithTimeIntervalSince1970:1*30*24*3600];//从1970经过若干秒后的时间
NSLog(@"%@",date1);//1970-01-31 00:00:00 +0000
NSDate *date2=[NSDate
dateWithTimeIntervalSinceNow:1*12*30*24*3600];//当前时间经过若干秒后的时间
NSLog(@"%@",date2);//2016-10-28 03:20:57 +0000
NSDate *date3=[date1
dateByAddingTimeInterval:60];//以某时间为准,加上若干秒后的时间
NSLog(@"%@",date3);//1970-01-31 00:01:00 +0000
NSTimeInterval interval11=[date1
timeIntervalSinceNow];//date1-当前时间=的时间
NSTimeInterval interval =[date
timeIntervalSince1970];//从1970年到date时间的秒数
NSTimeInterval interval1=[date1
timeIntervalSinceDate:date3];//date1-date3的时间
NSLog(@"%lf",interval1);//-60.000000
NSDate *later=[date
laterDate:date1];////获取两个时间较晚的(距离1970年时间较长的)
NSDate *earl=[date
earlierDate:date1];////获取两个时间较早的
NSLog(@"%@",earl);//1970-01-31 00:00:00 +0000
//格式化日期对象(把日期对象转换成字符串)
NSDateFormatter *dateformatter=[NSDateFormatter
new];
[dateformatter setDateFormat:@"yyyy年MM月dd日
HH:mm:ss a"];//
NSString *localDateTime=[dateformatter
stringFromDate:date];
NSLog(@"%@",localDateTime);//2015年11月03日
14:53:54
下午
//把日期格式的字符串转换成日期对象
NSString *myDateTime=@"2015/01/10 11:35:00";
NSDateFormatter *formatter=[NSDateFormatter
new];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSDate *newDate=[formatter
dateFromString:myDateTime];
NSLog(@"%@",newDate);//2015-01-10 03:35:00 +0000
/*
字符转换存在时区差,三种补偿方法 */
//
NSTimeZone *zone=[NSTimeZone
systemTimeZone];//获得系统时区
NSLog(@"%@",zone);//Asia/Shanghai (GMT+8) offset 28800
NSInteger timeSub=[zone
secondsFromGMT];//和格林尼治时间差
newDate=[newDate dateByAddingTimeInterval:timeSub];//补上时差
NSLog(@"%@",newDate);//2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[NSTimeZone
timeZoneWithName:@"UTC"]];//将时区设置为国际校准时间
NSDate *newDate1=[formatter
dateFromString:myDateTime];//字符串转换时间
NSLog(@"%@",newDate1);//2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[NSTimeZone
timeZoneForSecondsFromGMT:0]];//意思和第二种@“UTC”一样
@interface NSDate (NSDateCreation)
+ (instancetype)date;
//获取当前时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
// 返回以当前时间为基准,然后过了secs秒的时间
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
// 返回以1970年时间为基准,然后过了secs秒的时间
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
@end
@interface NSDate (NSExtendedDate)
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
//计算两个时间之间的间隔(秒数)
- (NSTimeInterval)timeIntervalSinceNow;
//返回给定时间与当前时间的时间差(秒数)
- (NSTimeInterval)timeIntervalSince1970;
//获取当前时间(秒数)
//表示从1970年到给定时间的秒数
- (id)dateByAddingTimeInterval:(NSTimeInterval)ti;
//以某时间为准,加上若干秒后的时间
- (NSDate *)earlierDate:(NSDate *)anotherDate;
//获取两个日期时间对象中较早的一个
- (NSDate *)laterDate:(NSDate *)anotherDate;
//获取两个日期时间对象中较晚的一个
- (NSComparisonResult)compare:(NSDate *)other;
- (BOOL)isEqualToDate:(NSDate *)otherDate;
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
@end
- (NSString *)stringFromDate:(NSDate *)date;
//格式化日期时间对象(把日期对象转换成字符串)
- (NSDate *)dateFromString:(NSString *)string;
//把日期格式的字符串转换成日期对象
- (void)setTimeZone:(NSTimeZone *)tz;//系统时区
- (void)setDateFormat:(NSString *)string;
//设置日期显示格式
date];//获取当前时间
NSLog(@"%@",date);//2015-11-03 03:15:10 +0000
NSDate *date1=[NSDate
dateWithTimeIntervalSince1970:1*30*24*3600];//从1970经过若干秒后的时间
NSLog(@"%@",date1);//1970-01-31 00:00:00 +0000
NSDate *date2=[NSDate
dateWithTimeIntervalSinceNow:1*12*30*24*3600];//当前时间经过若干秒后的时间
NSLog(@"%@",date2);//2016-10-28 03:20:57 +0000
NSDate *date3=[date1
dateByAddingTimeInterval:60];//以某时间为准,加上若干秒后的时间
NSLog(@"%@",date3);//1970-01-31 00:01:00 +0000
NSTimeInterval interval11=[date1
timeIntervalSinceNow];//date1-当前时间=的时间
NSTimeInterval interval =[date
timeIntervalSince1970];//从1970年到date时间的秒数
NSTimeInterval interval1=[date1
timeIntervalSinceDate:date3];//date1-date3的时间
NSLog(@"%lf",interval1);//-60.000000
NSDate *later=[date
laterDate:date1];////获取两个时间较晚的(距离1970年时间较长的)
NSDate *earl=[date
earlierDate:date1];////获取两个时间较早的
NSLog(@"%@",earl);//1970-01-31 00:00:00 +0000
//格式化日期对象(把日期对象转换成字符串)
NSDateFormatter *dateformatter=[NSDateFormatter
new];
[dateformatter setDateFormat:@"yyyy年MM月dd日
HH:mm:ss a"];//
NSString *localDateTime=[dateformatter
stringFromDate:date];
NSLog(@"%@",localDateTime);//2015年11月03日
14:53:54
下午
//把日期格式的字符串转换成日期对象
NSString *myDateTime=@"2015/01/10 11:35:00";
NSDateFormatter *formatter=[NSDateFormatter
new];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSDate *newDate=[formatter
dateFromString:myDateTime];
NSLog(@"%@",newDate);//2015-01-10 03:35:00 +0000
/*
字符转换存在时区差,三种补偿方法 */
//
NSTimeZone *zone=[NSTimeZone
systemTimeZone];//获得系统时区
NSLog(@"%@",zone);//Asia/Shanghai (GMT+8) offset 28800
NSInteger timeSub=[zone
secondsFromGMT];//和格林尼治时间差
newDate=[newDate dateByAddingTimeInterval:timeSub];//补上时差
NSLog(@"%@",newDate);//2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[NSTimeZone
timeZoneWithName:@"UTC"]];//将时区设置为国际校准时间
NSDate *newDate1=[formatter
dateFromString:myDateTime];//字符串转换时间
NSLog(@"%@",newDate1);//2015-01-10 11:35:00 +0000
//
[formatter setTimeZone:[NSTimeZone
timeZoneForSecondsFromGMT:0]];//意思和第二种@“UTC”一样
@interface NSDate (NSDateCreation)
+ (instancetype)date;
//获取当前时间
+ (instancetype)dateWithTimeIntervalSinceNow:(NSTimeInterval)secs;
// 返回以当前时间为基准,然后过了secs秒的时间
+ (instancetype)dateWithTimeIntervalSinceReferenceDate:(NSTimeInterval)ti;
+ (instancetype)dateWithTimeIntervalSince1970:(NSTimeInterval)secs;
// 返回以1970年时间为基准,然后过了secs秒的时间
- (instancetype)initWithTimeIntervalSinceNow:(NSTimeInterval)secs;
- (instancetype)initWithTimeIntervalSince1970:(NSTimeInterval)secs;
- (instancetype)initWithTimeInterval:(NSTimeInterval)secsToBeAdded sinceDate:(NSDate *)date;
@end
@interface NSDate (NSExtendedDate)
- (NSTimeInterval)timeIntervalSinceDate:(NSDate *)anotherDate;
//计算两个时间之间的间隔(秒数)
- (NSTimeInterval)timeIntervalSinceNow;
//返回给定时间与当前时间的时间差(秒数)
- (NSTimeInterval)timeIntervalSince1970;
//获取当前时间(秒数)
//表示从1970年到给定时间的秒数
- (id)dateByAddingTimeInterval:(NSTimeInterval)ti;
//以某时间为准,加上若干秒后的时间
- (NSDate *)earlierDate:(NSDate *)anotherDate;
//获取两个日期时间对象中较早的一个
- (NSDate *)laterDate:(NSDate *)anotherDate;
//获取两个日期时间对象中较晚的一个
- (NSComparisonResult)compare:(NSDate *)other;
- (BOOL)isEqualToDate:(NSDate *)otherDate;
+ (NSTimeInterval)timeIntervalSinceReferenceDate;
@end
- (NSString *)stringFromDate:(NSDate *)date;
//格式化日期时间对象(把日期对象转换成字符串)
- (NSDate *)dateFromString:(NSString *)string;
//把日期格式的字符串转换成日期对象
- (void)setTimeZone:(NSTimeZone *)tz;//系统时区
- (void)setDateFormat:(NSString *)string;
//设置日期显示格式
相关文章推荐
- object-C NSDate
- org.hibernate.TransientObjectException:
- Windows下编译objective-C
- Objective-C学习-单例以及通知中心(NotificationCenter)
- objectForKey与valueForKey在NSDictionary中的差异
- Objective-C之我对Runtime的理解(一)
- Objective-C之我对Runtime的理解(二)
- github Object-C javascript 消息互通 WebViewJavascriptBridge
- Objective-C学习笔记之NSData、NSDate
- Objective-C 身份证号码校验
- Object-C 多线程中锁的使用
- <iOS>enumerateKeysAndObjectsUsingBlock的用法
- Objective-C 程序设计 第八章
- ios-iPhone Objective-C EXC_BAD_ACCESS问题
- Does GATHER_STATS_JOB gather all objects’ stats every time?
- sqlserver system object type
- ios 关联对象运用 objc_setAssociatedObject
- Objective-C中的@property和@synthesize用法
- JSONObject文档
- NSMutableAttributedStrings - objectAtIndex:effectiveRange:: Out of bounds错误解决