最近项目中用到了周 月的具体时间,就找了一下资料发现系统提供了获取日期的API,
NSCalendar
这个类,这里我只计算了其中的一部分,有需要的同学可以根据自己的需求再修改.上两张效果图
- 1.获取当前的月份
ps
number
0 为当前月份 依次类推
+(NSString *)backToPassedTimeWithMonthNumber:(NSInteger)number
{
NSCalendar *cal = [NSCalendar currentCalendar];
NSDateComponents *components = [cal components:NSCalendarUnitMonth | NSCalendarUnitYear | NSCalendarUnitDay | NSCalendarUnitHour fromDate:[[NSDate alloc] init]];
[components setMonth:[components month] - number];
NSDate *monthDate = [cal dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-M"];
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zn_CN"]];
return [formatter stringFromDate:monthDate];
}
- 2.获取这个月有多少天
ps
dateStr 也可以修改为NSDate类型的 看自己需要
+(NSInteger)totaldaysInMonth:(NSString *)dateStr
{
NSDateFormatter *format=[[NSDateFormatter alloc] init];
[format setDateFormat:@"yyyy-MM-dd"];
NSDate *date=[format dateFromString:dateStr];
NSCalendar *c = [NSCalendar currentCalendar];
NSRange days = [c rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:date];
return days.length;
}
- 3.返回本周开始时间和结束时间
ps
number 同上
+(NSArray *)backToPassedTimeWithWeeksNumber:(NSInteger)number
{
NSDate *date = [[NSDate date] dateByAddingTimeInterval:- 7 * 24 * 3600 * number];
//滚动后,算出当前日期所在的周(周一-周日)
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *comp = [gregorian components:NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:date];
NSInteger daycount = [comp weekday] - 2;
NSDate *weekdaybegin=[date dateByAddingTimeInterval:-daycount*60*60*24];
NSDate *weekdayend = [date dateByAddingTimeInterval:(6-daycount)*60*60*24];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:weekdaybegin];
NSDateComponents *components1 = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:weekdayend];
NSDate *startDate = [calendar dateFromComponents:components];//这个不能改
NSDate *startDate1 = [calendar dateFromComponents:components1];
NSDate *endDate1 = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate1 options:0];
//获取今天0点到明天0点的时间
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
[formatter1 setDateFormat:@"yyyy-MM-dd"];
NSString *str1 = [formatter1 stringFromDate:startDate];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:@"yyyy-MM-dd"];
NSString *str2 = [formatter2 stringFromDate:endDate1];
NSArray *arr = @[str1,str2];
return arr;
}
- 4.获取周详细日期
+(NSMutableArray *)backWeeksTimeNumber:(NSInteger)number
{
NSDate *date = [[NSDate date] dateByAddingTimeInterval:- 7 * 24 * 3600 * number];
//滚动后,算出当前日期所在的周(周一-周日)
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *comp = [gregorian components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitWeekday fromDate:date];
NSInteger weekDay = [comp weekday]-1 ;
// 得到几号
NSInteger day = [comp day];
NSLog(@"weekDay:%ld day:%ld",weekDay,day);
// 计算当前日期和这周的星期一和星期天差的天数
long firstDiff,lastDiff;
if (weekDay == -1)
{ firstDiff = 1;
lastDiff = 0;
}
else
{
firstDiff = [gregorian firstWeekday] - weekDay;
lastDiff = 8 - weekDay;
}
NSMutableArray *currentWeeks = [self getCurrentWeeksWithFirstDiff:firstDiff lastDiff:lastDiff];
return currentWeeks;
}
+(NSMutableArray *)getCurrentWeeksWithFirstDiff:(NSInteger)first lastDiff:(NSInteger)last
{
NSMutableArray *eightArr = [[NSMutableArray alloc] init];
for (NSInteger i = first; i < last + 1; i ++)
{
//从现在开始的24小时
NSTimeInterval secondsPerDay = i * 24*60*60;
NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];
NSString *dateStr = [dateFormatter stringFromDate:curDate];
[dateFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zn_CN"]];
NSString *strTime = [NSString stringWithFormat:@"%@",dateStr];
[eightArr addObject:strTime];
}
return eightArr;
}
- 5.返回距离今天好多天
+(NSString *)backToPassedTimeWithDaysNumber:(NSInteger)number
{
NSString *str;
NSDate *date = [[NSDate alloc] initWithTimeIntervalSinceNow:-(60 * 24 * 60 * number)];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"M月dd日"];
[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"zn_CN"]];
str = [formatter stringFromDate:date];
return str;
}
- 6.获取某一天0点至下一天0点
+(NSArray *)backToPassedTimeIsNumber:(NSInteger)number
{
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *now = [[NSDate alloc] initWithTimeIntervalSinceNow:-(60 * 24 * 60 * number)];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:now];
NSDate *startDate = [calendar dateFromComponents:components];
NSDate *endDate = [calendar dateByAddingUnit:NSCalendarUnitDay value:1 toDate:startDate options:0];
//获取今天0点到明天0点的时间
NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
[formatter1 setDateFormat:@"yyyy-MM-dd"];
NSString *str1 = [formatter1 stringFromDate:startDate];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc]init];
[formatter2 setDateFormat:@"yyyy-MM-dd"];
NSString *str2 = [formatter2 stringFromDate:endDate];
NSArray *arr = @[str1,str2];
return arr;
}
- 7.根据日期获取星期
+(NSString*)weekdayStringFromDate:(NSDate*)inputDate
{
//NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期天", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"6", @"0", @"1", @"2", @"3", @"4", @"5", nil];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/SuZhou"];
[calendar setTimeZone: timeZone];
NSCalendarUnit calendarUnit = NSWeekdayCalendarUnit;
NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
return [weekdays objectAtIndex:theComponents.weekday];
}
好啦,具体的就这么多,希望能够帮助到需要的同学....