标签:
注意:
1.计算当月内的日历
// 获取当月的天数
- (NSInteger)getNumberOfDaysInMonth
{
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法
NSDate * currentDate = [NSDate date]; // 这个日期可以你自己给定
NSRange range = [calendar rangeOfUnit:NSDayCalendarUnit
inUnit: NSMonthCalendarUnit
forDate:currentDate];
return range.length;
}
/**
* 获取当月中所有天数是周几
*/
- (void) getAllDaysWithCalender
{
NSUInteger dayCount = [self getNumberOfDaysInMonth]; //一个月的总天数
NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
NSDate * currentDate = [NSDate date];
[formatter setDateFormat:@"yyyy-MM"];
NSString * str = [formatter stringFromDate:currentDate];
[formatter setDateFormat:@"yyyy-MM-dd"];
NSMutableArray * allDaysArray = [[NSMutableArray alloc] init];
for (NSInteger i = 1; i <= dayCount; i++) {
NSString * sr = [NSString stringWithFormat:@"%@-%ld",str,i];
NSDate *suDate = [formatter dateFromString:sr];
[allDaysArray addObject:[self getweekDayWithDate:suDate]];
}
NSLog(@"allDaysArray %@",allDaysArray);
}
/**
* 获得某天的数据
*
* 获取指定的日期是星期几
*/
- (id) getweekDayWithDate:(NSDate *) date
{
NSCalendar * calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 指定日历的算法
NSDateComponents *comps = [calendar components:NSWeekdayCalendarUnit fromDate:date];
// 1 是周日,2是周一 3.以此类推
return @([comps weekday]);
}
2.比较两个日期,compare:方法
返回NSComparisonResult枚举值
该枚举类型包含NSOrderedAscending、NSOrderedSame和NSOrderedDescending三个值
3.
NSDate -- 表示一个绝对的时间点
NSTimeZone -- 时区信息
NSLocale -- 本地化信息
NSDateComponents -- 一个封装了具体年月日、时秒分、周、季度等的类
NSCalendar -- 日历类,它提供了大部分的日期计算接口,并且允许您在NSDate和NSDateComponents之间转换
NSDateFormatter -- 用来在日期和字符串之间转换
一、NSDate
// 1.获取代表当前日期、时间的NSDate
NSDate* date1 = [NSDate date];
NSLog(@"%@" , date1);
// 2.获取从当前时间开始,一天之后的日期/3天之前的日期
NSDate* date2 = [[NSDate alloc]
initWithTimeIntervalSinceNow:3600*24];
NSLog(@"%@" , date2);
NSDate* date3 = [[NSDate alloc]
initWithTimeIntervalSinceNow: -3*3600*24];
NSLog(@"%@" , date3);
// 3.获取从1970年1月1日开始,20年之后的日期
NSDate* date4 = [NSDate dateWithTimeIntervalSince1970:
3600 * 24 * 366 * 20];
NSLog(@"%@" , date4);
NSLocale* cn = [NSLocale currentLocale];
NSLog(@"%@" , [date1 descriptionWithLocale:cn]);
// 4.获取两个日期之间较早的日期/ 获取两个日期之间较晚的日期
NSDate* earlier = [date1 earlierDate:date2];
NSDate* later = [date1 laterDate:date2];
switch ([date1 compare:date3])
{
case NSOrderedAscending:
NSLog(@"date1位于date3之前");
break;
case NSOrderedSame:
NSLog(@"date1与date3日期相等");
break;
case NSOrderedDescending:
NSLog(@"date1位于date3之后");
break;
}
// 5.获取两个时间之间的时间差/获取指定时间与现在的时间差
NSLog(@"date1与date3之间时间差%g秒" , [date1 timeIntervalSinceDate:date3]);
NSLog(@"date2与现在间时间差%g秒" , [date2 timeIntervalSinceNow]);
二、日期格式器
// 1.获取从1970年1月1日开始,20年之后的日期 NSDate* dt = [NSDate dateWithTimeIntervalSince1970:3600 * 24 * 366 * 20]; NSLocale* locales[] = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]; NSDateFormatter* df; //为上面NSLocale创建DateFormat对象 df = [[NSDateFormatter alloc] init]; // 2.设置NSDateFormatter的日期、时间风格 [df setDateStyle:NSDateFormatterShortStyle]; [df setTimeStyle:NSDateFormatterShortStyle]; // 设置自定义的格式器模板 [df2 setDateFormat:@"公元yyyy年MM月DD日 HH时mm分"]; // 3.执行格式化 NSLog(@"%@" , [df2 stringFromDate:dt]); NSString* dateStr = @"2013-03-02"; NSDateFormatter* df3 = [[NSDateFormatter alloc] init]; // 4.根据日期字符串的格式设置格式模板 [df3 setDateFormat:@"yyyy-MM-dd"]; // 将字符串转换为NSDate对象 NSDate* date2 = [df3 dateFromString: dateStr]; NSLog(@"%@" , date2);
三、日历
// 1.获取代表公历的Calendar对象 NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar]; // 2.获取当前日期 NSDate* dt = [NSDate date]; // 3.定义一个时间字段的旗标,指定将会获取指定年、月、日、时、分、秒的信息 unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit | NSWeekdayCalendarUnit; // 4.获取不同时间字段的信息 NSDateComponents* comp = [gregorian components: unitFlags fromDate:dt]; // 5.获取各时间字段的数值 NSLog(@"现在是%ld年" , comp.year); NSLog(@"现在是%ld月 " , comp.month); NSLog(@"现在是%ld日" , comp.day); NSLog(@"现在是%ld时" , comp.hour); NSLog(@"现在是%ld分" , comp.minute); NSLog(@"现在是%ld秒" , comp.second); NSLog(@"现在是星期%ld" , comp.weekday); // 6.再次创建一个NSDateComponents对象 NSDateComponents* comp2 = [[NSDateComponents alloc] init]; // 7.设置各时间字段的数值 comp2.year = 2013; comp2.month = 4; comp2.day = 5; comp2.hour = 18; comp2.minute = 34; // 8.通过NSDateComponents所包含的时间字段的数值来恢复NSDate对象 NSDate *date = [gregorian dateFromComponents:comp2]; NSLog(@"获取的日期为:%@" , date);
标签:
原文地址:http://www.cnblogs.com/lsa0913/p/5723950.html