nsdate 和 日历

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);//2014730 星期三

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:@"yyyyMMdd hhmmss"];

        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上面。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 Objective-C 代码输出一个月份的日历: ```objc // 获取当前日期 NSDate *today = [NSDate date]; NSCalendar *calendar = [NSCalendar currentCalendar]; NSDateComponents *components = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:today]; NSInteger year = [components year]; NSInteger month = [components month]; // 设置日期格式化对象 NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat:@"MMMM yyyy"]; // 获取当前月份第一天的日期 NSDateComponents *firstDayComponents = [[NSDateComponents alloc] init]; [firstDayComponents setYear:year]; [firstDayComponents setMonth:month]; [firstDayComponents setDay:1]; NSDate *firstDayOfMonth = [calendar dateFromComponents:firstDayComponents]; // 获取当前月份的天数 NSRange daysInMonth = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:firstDayOfMonth]; // 输出日历 NSString *header = [dateFormatter stringFromDate:firstDayOfMonth]; NSLog(@"%@", header); NSLog(@"Su Mo Tu We Th Fr Sa"); NSDateComponents *currentDateComponents = [[NSDateComponents alloc] init]; [currentDateComponents setDay:1]; [currentDateComponents setMonth:month]; [currentDateComponents setYear:year]; NSInteger weekday = [calendar component:NSCalendarUnitWeekday fromDate:firstDayOfMonth]; for (NSInteger i = 1; i < weekday; i++) { printf(" "); } for (NSInteger i = 1; i <= daysInMonth.length; i++) { printf("%2ld ", (long)i); if (i == daysInMonth.length) { printf("\n"); break; } [currentDateComponents setDay:i + 1]; NSDate *currentDate = [calendar dateFromComponents:currentDateComponents]; NSInteger currentWeekday = [calendar component:NSCalendarUnitWeekday fromDate:currentDate]; if (currentWeekday == 1) { printf("\n"); } } ``` 该代码会输出当前月份的日历,例如: ``` May 2021 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ``` 你可以根据需要修改年月来输出不同月份的日历

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值