Objective-c 日期 NSDate 基本够做一个简单的日历
2014-10-01 18:55
375 查看
转载自:http://blog.sina.com.cn/s/blog_5fb39f9101017am9.html
//获得当前日期
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps;
//本年
comps = [calendar components:(NSYearCalendarUnit) fromDate:date];
NSInteger year = [comps year];
//本月
comps = [calendar components:(NSMonthCalendarUnit) fromDate:date];
NSInteger month = [comps month];
//本月第一天的星期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"M/d/yyyy"];
NSString *firstDay = [NSString stringWithFormat:@"%d/%d/%d",month,1,year];
date = [formatter dateFromString:firstDay];
[formatter release];
comps = [calendar components:(NSWeekdayCalendarUnit) fromDate:date];
NSInteger weekday = [comps weekday];
//下月第一天
NSDateComponents *c1 = [[NSDateComponents alloc] init];
[c1 setMonth:1];
NSDate *date2 = [calendar dateByAddingComponents:c1 toDate:date options:0];
[c1 release];
//本月最后一天
NSDateComponents *c2 = [[NSDateComponents alloc] init];
[c2 setDay:-1];
NSDate *date3 = [calendar dateByAddingComponents:c2 toDate:date2 options:0];
[c2 release];
comps = [calendar components:(NSDayCalendarUnit) fromDate:date3];
NSInteger maxDays = [comps day];
//获得当前日期
NSDate *date = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comps;
//本年
comps = [calendar components:(NSYearCalendarUnit) fromDate:date];
NSInteger year = [comps year];
//本月
comps = [calendar components:(NSMonthCalendarUnit) fromDate:date];
NSInteger month = [comps month];
//本月第一天的星期
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone localTimeZone];
[formatter setTimeZone:timeZone];
[formatter setDateFormat:@"M/d/yyyy"];
NSString *firstDay = [NSString stringWithFormat:@"%d/%d/%d",month,1,year];
date = [formatter dateFromString:firstDay];
[formatter release];
comps = [calendar components:(NSWeekdayCalendarUnit) fromDate:date];
NSInteger weekday = [comps weekday];
//下月第一天
NSDateComponents *c1 = [[NSDateComponents alloc] init];
[c1 setMonth:1];
NSDate *date2 = [calendar dateByAddingComponents:c1 toDate:date options:0];
[c1 release];
//本月最后一天
NSDateComponents *c2 = [[NSDateComponents alloc] init];
[c2 setDay:-1];
NSDate *date3 = [calendar dateByAddingComponents:c2 toDate:date2 options:0];
[c2 release];
comps = [calendar components:(NSDayCalendarUnit) fromDate:date3];
NSInteger maxDays = [comps day];
相关文章推荐
- 闲来无事写的一个简单的JavaScript日历
- 一个简单的日历控件
- (1)一步一步开发一个简单二维CAD之基本框架
- 一个简单的JavaScript日期计算算法
- 一个简单的JavaScript 日期计算算法
- 通过一个简单的SWING日历BEAN开发学习Calendar类的使用(1)Calendar概述
- 重新整理一个简单的JS日历控件
- 编写、编译、安装、移除一个内核模块的基本过程,不能再简单了的
- 一个简单的邮件查找类,用来查找特定主题的邮件,特定日期,或者是包含有附件的邮件
- 一个简单横向javascript日期控件
- 一个显示日期的简单小程序,主要是学习静态变量的用法
- 设计一个简单的日期选择输入部件
- 一个简单的日期选择框(支持IE,FF,Opera)
- 一个简单的日历控件
- 做一个有基本printf和clearscreen函数的简单c内核
- 写了一个简单的日期检测工具
- 一个简单的日期控件
- 一个日历程序(基本每一行都带有注释)
- [java]一个简单的程序巩固基本概念
- Objective-c NSDate 取时间,日期,星期,毫秒等