的天数,以一个日期与当前日期的

时间:2022-12-25 13:56:45

//返回0 当天,1昨天。2前天,以此类推

//之前那样用时间差来整除一天的秒数,计算出来的天数有误差

+ (int)getDaysFromAnotherDay:(longlong)date {//date是时间戳

//    return [self getDaysFrom1970:date] - [self getDaysFrom1970:[[NSDate date] timeIntervalSince1970]];

//    NSDateComponents *comps = [[NSDateComponents alloc] init];

//    [comps setDay:[NSDateUtils getDay:date]];

//    [comps setMonth:[NSDateUtils getMonth:date]];

//    [comps setYear:[NSDateUtils getYear:date]];

//    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

//    NSDate *endDate = [[NSDate alloc] init];

//    NSDate *startDate = [gregorian dateFromComponents:comps];

//    [comps release];

//    unsigned int unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit;

//    NSDateComponents *comps2 = [gregorian components:unitFlags fromDate:startDate toDate:endDate options:0];

//    [gregorian release];

//    [endDate release];

//    int days = [comps2 day];

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];

    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    [dateFormatter setCalendar: [NSCalendar autoupdatingCurrentCalendar]];

    NSDate *today = [dateFormatter dateFromString:[dateFormatter stringFromDate:[NSDate date]]];

    NSDate *startDate = [dateFormatter dateFromString:[NSDateUtils getyyyyMMddDate:date]];

    NSDate *formatterStartDate = [dateFormatter dateFromString:[dateFormatter stringFromDate:startDate]];

    NSTimeInterval interval = [today timeIntervalSinceDate:formatterStartDate];

    NSInteger days = ((NSInteger)interval)/(3600*24);

    return days;

}


+ (int)getYear:(longlong)date {

    return [[selfgetNSDateComponents:date]year];

}


+ (int)getMonth:(longlong)date {

    return [[selfgetNSDateComponents:date]month];

}


+ (int)getDay:(longlong)date {

    return [[selfgetNSDateComponents:date]day];

}


+ (NSDateComponents *)getNSDateComponents:(longlong)date {

    NSInteger unitFlags =NSYearCalendarUnit | NSMonthCalendarUnit |NSDayCalendarUnit | NSWeekdayCalendarUnit |NSHourCalendarUnit | NSMinuteCalendarUnit |NSSecondCalendarUnit;

    NSDateComponents *comps = [[selfshareNSCalendar]components:unitFlags fromDate:[NSDatedateWithTimeIntervalSince1970:date]];

   return comps;

}



版权声明:本文博客原创文章,博客,未经同意,不得转载。