//获取本周周一的时间
+(NSDate *)getMondayTime{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
// 获取今天是周几
NSInteger weekDay = [comp weekday];
// 获取几天是几号
NSInteger day = [comp day];
// 计算当前日期和本周的星期一和星期天相差天数
long firstDiff,lastDiff;
if (weekDay == 1){
firstDiff = -6;
lastDiff = 0;
}
else{
firstDiff = [calendar firstWeekday] - weekDay + 1;
lastDiff = 8 - weekDay;
}
// 在当前日期(去掉时分秒)基础上加上差的天数
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
[firstDayComp setDay:day + firstDiff];
return [calendar dateFromComponents:firstDayComp];
}
//上周一
+(NSDate *)getLastMondayTime{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
//获取今天是周几
NSInteger weekDay = [comp weekday];
//获取某天是几号
NSInteger day = [comp day];
//计算当前日期和上周的星期一和星期天相差天数
long firstDiff,lastDiff;
if (weekDay == 1) {
firstDiff = -13;
lastDiff = 0;
}
else{
firstDiff = [calendar firstWeekday] - weekDay +1-7;
lastDiff = 8 - weekDay;
}
//在当前日期基础上加上时间差的天数
NSDateComponents *firstDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
[firstDayComp setDay:day + firstDiff];
return [calendar dateFromComponents:firstDayComp];
}
//上周日
+(NSDate *)getLastSundayTime{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
//获取今天是周几
NSInteger weekDay = [comp weekday];
//获取某天是几号
NSInteger day = [comp day];
//在当前日期基础上加上时间差的天数
NSDateComponents *lastDayComp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:nowDate];
[lastDayComp setDay:day-weekDay+1];
return [calendar dateFromComponents:lastDayComp];
}
/**
上月第一天
*/
+(NSDate *)getLastMonthFirstDayTime{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
//获取某天是几号
NSInteger day = [comp day];
NSDate * currentDate = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60*(day-1))];
NSDateComponents *lastMonthComps = [[NSDateComponents alloc] init];
//[lastMonthComps setYear:1]; // year = 1表示1年后的时间 year = -1为1年前的日期,month day 类推
[lastMonthComps setMonth:-1];
return [calendar dateByAddingComponents:lastMonthComps toDate:currentDate options:0];;
}
/**
上月最后一天
*/
+(NSDate *)getLastMonthLastDayTime{
NSDate *nowDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *comp = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitWeekday | NSCalendarUnitDay fromDate:nowDate];
//获取某天是几号
NSInteger day = [comp day];
NSDate * lastMonthEndDay = [NSDate dateWithTimeIntervalSinceNow:-(24*60*60*day)];
return lastMonthEndDay;
}
/**
本月第一天
*/
+(NSDate *)getMonthFirstDayTime{
NSDate *newDate=[NSDate date];
double interval = 0;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *firstDate = nil;
BOOL OK = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];
if (OK) {
return [firstDate dateByAddingTimeInterval:interval - 1];
}
else{
return newDate;
}
}
本周 上周 本月 上月
最新推荐文章于 2022-03-21 16:26:56 发布