// 获取当前月初一的时间戳
+ (NSString *)getMonthFirstDayTime{
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
NSInteger year=[components year];
NSInteger month=[components month];
//构造当月的1号时间
NSDateComponents *firstDayCurrentMonth = [[NSDateComponents alloc] init];
[firstDayCurrentMonth setYear:year];
[firstDayCurrentMonth setMonth:month];
[firstDayCurrentMonth setDay:1];
//当月1号
NSDate *firstDayOfCurrentMonth = [calendar dateFromComponents:firstDayCurrentMonth];
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[firstDayOfCurrentMonth timeIntervalSince1970]];
return timeStr;
}
// 获取上月初一的时间戳
+ (NSString *)getLastMonthFirstDayTime{
NSDate *currentDate = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
NSInteger year=[components year];
NSInteger month=[components month];
//构造上月1号时间
month --;
//获取上月月份 没的说
if (month == 0) {
month = 12;
year--;
}
NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
[dateComponents setYear:year];
[dateComponents setMonth:month];
[dateComponents setDay:1];
//上月1号时间
NSDate *firstDayOfLastMonth = [calendar dateFromComponents:dateComponents];
NSString *timeStr = [NSString stringWithFormat:@"%ld", (long)[firstDayOfLastMonth timeIntervalSince1970]];
return timeStr;
}
// 上月的结束时间:
// 上个月的最后一天的最后一秒
NSDate *lastDayOfLastlMonth = [firstDayOfCurrentMonth dateByAddingTimeInterval:-1];