NSDateFormatter

指定⽇日期格式:
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

⽇日期转化为字符串:
NSDateFormatter*formatter =
[[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
NSString *date
String=[formatter stringFromDate: [NSDate date]]; 

 

NSDate 处理日期或者时间的一个类,用于获取日期

        NSDate * date = [NSDate date];//是个类方法

全球份24个时区,我们控制台打印的是0时区的时间

        //获取当前的日期时间

        NSDate * date = [NSDate date];//是个类方法全球份24个时区,我们控制台打印的是0时区的时间

        NSLog(@"%@",date);

        //获取明天此时的时间 ,计算明天距离此时的时间,以及本单位秒来计算的

        NSDate * date1 = [NSDate dateWithTimeIntervalSinceNow:24*60*60];

        NSLog(@"tomorrow = %@",date1);

 //获取昨天的日期

        NSDate * yesterday = [NSDate dateWithTimeIntervalSinceNow:-24*60*60];

        NSLog(@"yesterday = %@",yesterday);

 

dateWithTimeIntervalSinceNow方法

        //获取1970.1.1到此时的时间间隔

        NSTimeInterval timeInterval = [datenow timeIntervalSince1970];

        NSLog(@"%f",timeInterval);//结果是以秒为单位

//获取两个日期的时间间隔

        NSTimeInterval interval = [datenow timeIntervalSinceDate:tomorrow];//用前面日期 - 后面的日期 datenow - tomorrow)以秒为单位

        NSLog(@"%f",interval);

        NSLog(@"获取两个日期的时间间隔 %.1f ",interval/24/60/60);//计算相差天数

        

        //获取据当前日期的时间间隔

        NSTimeInterval currentInterval = [yesterday timeIntervalSinceNow];

        NSLog(@"据当前日期的时间间隔 %.1f ",currentInterval/24/60/60);

        

        //两个日期之间的比较(返回一个枚举值)默认是升序比较

        NSComparisonResult result = [yesterday compare:tomorrow];

        NSLog(@"%ld",result);

        

        //判断两个日期是否相等(返回BOOL)

        BOOL isEqual = [tomorrow isEqualToDate:yesterday];

        if (isEqual) {

            NSLog(@"两个日期相等");

        }else{

            NSLog(@"两个日期不相等");

        }

 

//练习 控制台输入一个时间间隔 如果据此时差值在60 就提示刚刚”  如果在60秒之外小于3600 就提示“XX 分钟前,如果大于 3600 就输出 “XX 小时前

        NSTimeInterval time = 0;

        printf("请输入时间");

        scanf("%lf",&time);

        if (time < 60) {

            NSLog(@"刚刚");

        }

        if(time > 60 && time < 3600){

            NSLog(@"%.f分钟前",time/60);

        }

        if (time > 3600) {

            NSLog(@"%.f小时前",time/60/60);

        }

时区的转化那点事       http://my.oschina.net/yongbin45/blog/151376

 下午所学:

分类:1.作用是为不知道源代码的类提供方法(扩展功能)

 2. 分类并不是一个类,只是为我们原有的类扩展功能,就是添加方法,而且只能添加方法,不能添加实例变量

cmd + ctrl +方向键 对(.h 与 .m文件的切换)

对于分类的使用,也需要在主文件里导入

 Extension 与 category 的区别:前者是一个类的私有方法,后者是为不知道源代码的类的方法的扩展

 1.制定协议内容

         2.设置代理人对象

         3.制定代理人

         4.代理人对象服从协议

         5.代理人实现协议中的方法

         6.通知代理人执行

//笑笑糖语法只能给不可变数组用

//代理delegate

 1 //
 2 //  main.m
 3 //  Protocol(协议)
 4 //
 5 //
 8 
 9 #import <Foundation/Foundation.h>
10 #import "Man.h"
11 #import "Woman.h"
12 #import "BabySister.h"
13 #import "Mother.h"
14 
15 //1.制定协议
16 //2。对象遵循协议
17 //3.设置代理人
18 //4.代理人实现协议中的方法
19 //5.通知代理人去执行
20 //6.代理人去执行协议
21 int main(int argc, const char * argv[]) {
22     @autoreleasepool {
23         // insert code here...
24         NSLog(@"故事开始, World!");
25         NSLog(@"在某一天,一个风尘仆仆的美男子,出现在一个漆黑的夜晚....");
26         NSLog(@"突然...");
27         NSLog(@"前面出现了一个美如天仙的如花姑娘。。。");
28         NSLog(@"很快,两人就这样认识了");
29         NSLog(@"很快,两人就步入了婚姻的殿堂");
30         Woman *honey = [[Woman alloc]initWithName:@"shuhao" age:@"22"];
31         Man *man = [[Man alloc]initWithName:@"pengpeng" age:@"24"];
32         NSLog(@"shuhao:你要是帮我做这几件事,我就嫁给你");
33         NSLog(@"pengpeng :I DO");
34         //设置代理人
35         [honey setDelegate:man];
36         //通知代理人执行
37         [honey comeon];
38         
39         
40         Mother * mother = [[Mother alloc]initWithName:@"mom" age:@"35"];
41         BabySister * sister = [[BabySister alloc]initWithName:@"xiao" age:@"23"];
42 
43        
44         
45     }
46     return 0;
47 }
View Code  main.m 文件

相关文章: