iOS 获取当前日期是周几、每月最后一天

获取当前日期月份的第一天/最后一天日期
获取当前日期是周几

OC写法

//获取日期date对应月的第一天日期
+ (NSString *)getMonthFirstDayWithDate:(NSDate *)date format:(NSString *)aformat;
//获取日期date对应月的最后一天日期
+ (NSString *)getMonthLastDayWithDate:(NSDate *)date format:(NSString *)aformat;
//获取对应日期是周几
+ (NSInteger)getWeekDayFromDate:(NSDate *)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 @"";
}

+ (NSInteger)getWeekDayFromDate:(NSDate *)date{
    NSArray *tempWeek = @[@"7",@"1",@"2",@"3",@"4",@"5",@"6"];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    
    NSInteger unitFlags = NSCalendarUnitYear |NSCalendarUnitMonth | NSCalendarUnitDay |NSCalendarUnitWeekday | NSCalendarUnitHour |NSCalendarUnitMinute |NSCalendarUnitSecond;
    
    comps = [calendar components:unitFlags fromDate:date];
    //  1、2、3、4、5、6、7 分别对应 周日、周一、周二、周三、周四、周五、周六
    NSInteger week = [comps weekday];
    NSLog(@"---%ld",week);
  //  调整后 1 代表 周一
    return  [tempWeek[week-1] integerValue] ;
    
}



Swift 写法

struct DateRange {
    var startDate : String
    var endDate   : String
    
    init(startDate:String? ,endDate:String?) {
        self.startDate = startDate ?? ""
        self.endDate   = endDate ?? ""
    }
}
  • 获取某月的第一天 和 最后一天
    /// 获取某月的第一天 和 最后一天
    /// - parameter date: 时间,默认当前时间
    /// - parameter interval: 间隔 。默认为0
    /// - parameter format: 日期格式
    ///
    /// - 间隔传入 1  即为传入日期的下一个月  ,-1 上一个月
    
    class func firstDay_LastDayOfMounth(date : Date = Date() ,interval: Int = 0, format: String = "yyyy-MM-dd") -> DateRange {
        let newDate = date
        
        let ca = Calendar.init(identifier: .gregorian)
        var lastMonthComps = DateComponents()
        lastMonthComps.month = interval
        let curDate = ca.date(byAdding: lastMonthComps, to: newDate)
        
        var interval:Double = 0
        var firstDate:Date = Date()
        var lastDate :Date = Date()
        
        let calendar = NSCalendar.current
        let bl = calendar.dateInterval(of: .month, start: &firstDate, interval: &interval, for: curDate!)
        
        if bl {
            lastDate = firstDate.addingTimeInterval(interval-1)
        }
        
        let firstStr = ZLDateTimeTool.stringFromDate(date: firstDate, format: format)
        let lastStr  = ZLDateTimeTool.stringFromDate(date: lastDate, format: format)
        
        let range = DateRange.init(startDate: firstStr, endDate: lastStr)
        return range
    }
  • 返回对应日期是周几
///  - 返回对应日期是周几
     class func getWeekDayByDate(date:Date) -> Int{
        guard let calendar = NSCalendar(identifier: NSCalendar.Identifier.gregorian) else {
            return 1
        }
        let components = calendar.components([.weekOfYear,.weekOfMonth,.weekday,.weekdayOrdinal], from: date)
        /*
        //今年的第几周
        let weekOfYear = components.weekOfYear!
        //这个月第几周
        let weekOfMonth = components.weekOfMonth!
        //这个月第几周
        let weekdayOrdinal = components.weekdayOrdinal!*/
        //周几
        let weekday = components.weekday!
        //调整前  1、2、3、4、5、6、7 分别对应 周日、周一、周二、周三、周四、周五、周六
        let cnWeekday = [7, 1, 2, 3, 4, 5, 6]
        //调整后  1、2、3、4、5、6、7 分别对应 周一、周二、周三、周四、周五、周六、周日
        return cnWeekday[weekday-1]
    }
    

你可能感兴趣的:(iOS 获取当前日期是周几、每月最后一天)