#import <Foundation/Foundation.h> #import "monthModel.h" #import "dayModel.h" #import "SolarAndLunerHeader.h" @interface PublicUtils : NSObject #pragma mark将时间本地化 +(NSDate *)getLocalDataWithData:(NSDate *)date; #pragma mark获取天数 +(NSRange)getRangefromSmallUtilInBigUtil:(NSDate *)date; #pragma mark获取小单位在大单位中得系数,主要用于根据日期获得今天是星期几,默认setfirstweekday为1就是从周日开头的 +(NSInteger)getCountFromSmallUtilInBigUtil:(NSDate *)date; #pragma mark 获得当前月第一天的日期 +(NSDate *)getCurrentMonthFirstDay; #pragma mark 获得次月第一天的日期 +(NSDate *)getnextMonthFirstDay; #pragma mark 组装一个空白数据,当月到次月的数据没有变化的封装,具体细节由服务器中取出的数据再维护一下得出真实的数据 +(NSMutableArray *)createFixedDData; #pragma mark 同步月份数据,只是星期同步 +(void)copyWeekWithDictionary:(NSMutableArray *)array; @end
/* 1.date 转 NSDateComponents 通过日历会将时间本地化 2.反过来,NSDateComponents 转date 会逆向行为会讲转出来的时间会转到0时区的时间 总结:本地时间转NSDateComponents用本地化日历转的话,最好将日历的时区设置为0时区,否则就会2次本地化时间出错 反过来也一样。 */ #import "PublicUtils.h" @implementation PublicUtils #pragma mark将时间本地化 +(NSDate *)getLocalDataWithData:(NSDate *)date{ NSTimeZone *timezone= [NSTimeZone systemTimeZone]; NSInteger interval= [timezone secondsFromGMT]; return [date dateByAddingTimeInterval:interval]; } #pragma mark获取天数 +(NSRange)getRangefromSmallUtilInBigUtil:(NSDate *)date{ NSCalendar *calendar = [NSCalendar currentCalendar]; NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date]; return range; } #pragma mark获取小单位在大单位中得系数,主要用于根据日期获得今天是星期几,默认setfirstweekday为1就是从周日开头的 +(NSInteger)getCountFromSmallUtilInBigUtil:(NSDate *)date{ NSCalendar *calendar = [NSCalendar currentCalendar]; return [calendar ordinalityOfUnit:NSCalendarUnitWeekday inUnit:NSCalendarUnitWeekOfMonth forDate:date]; } #pragma mark 获得当前月第一天的日期 +(NSDate *)getCurrentMonthFirstDay{ NSDate *date= [NSDate date]; NSCalendar *calender= [NSCalendar currentCalendar]; NSDateComponents *compt= [calender components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date]; [compt setDay:1]; NSDate *currentdate= [calender dateFromComponents:compt]; return [PublicUtils getLocalDataWithData:currentdate]; } #pragma mark 获得次月第一天的日期 +(NSDate *)getnextMonthFirstDay{ NSDate *date= [NSDate date]; NSCalendar *calender= [NSCalendar currentCalendar];//同比下面这个不用转成0时区 NSDateComponents *compt= [calender components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay) fromDate:date]; if (compt.month==12) { [compt setMonth:compt.month+1]; [compt setYear:compt.year+1]; }else { [compt setMonth:compt.month+1]; } [compt setDay:1]; NSDate *currentdate= [calender dateFromComponents:compt]; return [PublicUtils getLocalDataWithData:currentdate]; } #pragma mark 从日期里面获取各个元素 +(NSDateComponents *)changeToCompontsFromDate:(NSDate *)date{ NSCalendar *calender= [NSCalendar currentCalendar]; [calender setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];//(设置这个的目的,传进来的是一个本地时间,再用本地是日历转一次,就会造成2次本地化就会有误差,所以将日历的时区转成0时区,来避免2次转换) NSDateComponents *compt= [calender components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:date]; return compt; }