NSDate用法:
1.获取时间
NSDate *date = [NSDate date];
//标准时间
NSLog(@"%@",date);
1.2.得到昨天的时间
得到昨天的时间
//做一天意味着24小时之前的时间,就等于24*60*60秒
//我们时间间隔使用秒来计算的
//dateWithTimeIntervalSinceNow就是计算从现在开始某一个间隔的时间。
//参数就是时间间隔,正的想以后走,负的向以前走。
NSDate *yesterdayDate = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60)];
NSLog(@"%@",yesterdayDate);
NSDate *tomorrowDate = [NSDate dateWithTimeIntervalSinceNow:+(32*60*60+30*60)];//得到明天的中国时区时间
1.3. 创建一个formate对象之后,用这个对象来设置我们的时间格式(格式化)dateStyle/timeStyle
NSDateFormatter *formDate = [[NSDateFormatter alloc] init];
1.4.设置日期类型
[formDate setDateStyle:NSDateFormatterFullStyle];(此处有几种风格,按需任选。)
1.5.使用format 吧date转成nsstring,同时你找上面设置好的类型来实现
NSString *formate = [formDate stringFromDate:date];
NSLog(@"%@",formate);//2014年7月30日 星期三
1.6.设置日期里面格式 MM表示月,mm表示分钟。
[formDate setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *formDtae2 = [formDate stringFromDate:date];
NSLog(@"%@",formDtae2);
1.7.计算时间差
NSDate *bDate = [NSDate dateWithTimeIntervalSinceNow:-(4*60*60)];
NSLog(@"%@",bDate);
// NSTimeInterval timeInt = [bDate timeIntervalSinceNow];
NSTimeInterval timeInt1 = [date timeIntervalSinceDate:bDate];
NSLog(@"%f",timeInt1);
2.日历
//NSCalendar
2.1.实例化一个日历对象,用一个标识来初始化它。
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
2.2.实例化一个日期对象,这个日期不是NSDate,是个各项数字集合的对象。
NSDateComponents *com = [[NSDateComponents alloc] init
];
2.3.做一个变量,这个变量保存了我们想要通过上面第二部的日期对象获取什么内容的数据。
NSInteger iWantContent = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit;
//上面的内容自由增删,想获取什么,就些什么。
2.4.用第二部的日期对象把这些内容提取出来
//返回值是第二步的日起对象类型,调用方法的对象是日历对象,第一个参数是第三部我们想获取的内容的集合变量,第二个参数是一个时间。
com = [calendar components:iWantContent fromDate:date];
2.5.吧获取到的内容取出来。用com调用不同值的方法(年月日周时分秒)
NSInteger week = [com weekday];
//还有六个属性都可以这么取。
NSInteger year = [com year];
NSInteger month = [com month];
NSInteger day = [com day];
NSInteger hour = [com hour];
NSInteger minute = [com minute];
NSInteger second = [com second];
NSLog(@"%lu-%lu-%lu %lu %lu:%lu:%lu",year,month,week,day,hour,minute,second);
//eg:计算8月2日的时间(通过今天的时间)
NSDate *qixiDay = [NSDate dateWithTimeIntervalSinceNow:+(3*24*60*60+8*60)];
NSDateFormatter *formDate = [[NSDateFormatter alloc] init];
// [formDate setDateStyle:NSDateFormatterLongStyle];
[formDate setDateFormat:@"yyyy年MM月dd日 hh时mm分ss秒"];
NSString *strDate = [formDate stringFromDate:qixiDay];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *com = [[NSDateComponents alloc] init];
NSInteger iWantContent = NSYearCalendarUnit |NSMonthCalendarUnit|NSDayCalendarUnit | NSWeekdayCalendarUnit | NSHourCalendarUnit |NSMinuteCalendarUnit | NSSecondCalendarUnit;
com = [calendar components:iWantContent fromDate:qixiDay];
NSInteger year = [com year];
NSInteger month = [com month];
NSInteger week = [com week];
NSInteger day = [com day];
NSInteger hour = [com hour];
NSInteger minute = [com minute];
NSInteger second = [com second];
NSLog(@"%lu年%lu月%lu日 第%lu周 %lu:%lu:%lu",year,month,week,day,hour,minute,second);
NSLog(@"%@",strDate);
3.协议(详见7.30 protocal程序)
方法声明列表
分为非正式协议 @optional
和正式协议@required
@protocol
在.h文件中引入协议并且包含,在.m中实现协议中的方法
协议可以不写一个单独文件,可以写在@interface上面。