美文网首页
获取对应月的第一天日期或者最后一天日期

获取对应月的第一天日期或者最后一天日期

作者: 东部落的太阳 | 来源:发表于2022-09-29 16:41 被阅读0次

    //获取日期date对应月的第一天日期//获取日期date对应月的最后一天日期

    + (NSString *)getMonthFirstDayWithDate:(NSDate *)date format:(NSString *)aformat{

        NSDate *newDate = date;

        double interval = 0;

        NSDate *firstDate = nil;

        NSCalendar *calendar = [NSCalendar currentCalendar];

        BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];

        if (bl) {

            NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];

            [myDateFormatter setDateFormat:aformat];

            NSString *firstString = [myDateFormatter stringFromDate: firstDate];

            return firstString;

        }

        return @"";

    }

    + (NSString *)getMonthLastDayWithDate:(NSDate *)date format:(NSString *)aformat{

        NSDate *newDate = date;

        double interval = 0;

        NSDate *firstDate = nil;

        NSDate *lastDate = nil;

        NSCalendar *calendar = [NSCalendar currentCalendar];

        BOOL bl = [calendar rangeOfUnit:NSCalendarUnitMonth startDate:& firstDate interval:&interval forDate:newDate];

        if (bl) {

            lastDate = [firstDate dateByAddingTimeInterval:interval - 1];

            NSDateFormatter *myDateFormatter = [[NSDateFormatter alloc] init];

            [myDateFormatter setDateFormat:aformat];

            NSString *lastString = [myDateFormatter stringFromDate: lastDate];

            return lastString;

        }

        return @"";

    }

    相关文章

      网友评论

          本文标题:获取对应月的第一天日期或者最后一天日期

          本文链接:https://www.haomeiwen.com/subject/qdskartx.html