MatthewYork / DateTools

Dates and times made easy in iOS
MIT License
7.22k stars 952 forks source link

Swift 2.3 -> Swift 4.0 Help to transform code #265

Closed NikKovIos closed 6 years ago

NikKovIos commented 6 years ago

Hello there! I'm using the last Date tools and need to port the old functions to new. Help me please with it.

func thisWeekStart() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.weekday() - 2)
    }

    func thisMonthStart() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.day() - 1)
    }

    func thisYearStart() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.dayOfYear() - 1)
    }

    func lastWeekStart() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.weekday() - 2).dateBySubtractingWeeks(1)
    }

    func lastWeekEnd() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.weekday() - 1)
    }

    func lastMonthStart() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.day() - 1).dateBySubtractingMonths(1)
    }

    func lastMonthEnd() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.day())
    }

    func lastYearStart() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.dayOfYear() - 1).dateBySubtractingYears(1)
    }

    func lastYearEnd() -> NSDate {
        let date = now()
        return date.dateBySubtractingDays(date.dayOfYear())
    }

There is my solution, but i don't think it's correct:

func thisWeekStart() -> Date {
        let date = now()
        return date - (date.weekday - 2)
    }

    func thisMonthStart() -> Date {
        let date = now()
        return date - (date.day - 1)
    }

    func thisYearStart() -> Date {
        let date = now()
        return date - (date.year.days.days - 1)
    }

    func lastWeekStart() -> Date {
        let date = now()
        return date - ((date.weekday - 2) - 1.weeks.days)
    }

    func lastWeekEnd() -> Date {
        let date = now()
        return date - (date.weekday - 1)
    }

    func lastMonthStart() -> Date {
        let date = now()
        return date - ((date.day - 1) - 1.months.days)
    }

    func lastMonthEnd() -> Date {
        let date = now()
        return date - date.day
    }

    func lastYearStart() -> Date {
        let date = now()
        return date - ((date.year.days.days - 1) - 1.years.days)
    }

    func lastYearEnd() -> Date {
        let date = now()
        return date - date.year.days.days
    }
NikKovIos commented 6 years ago

Here is my solution:

let calendar = Calendar.current

    func dateWithComponents(_ components: Set<Calendar.Component>, from date: Date? = nil) -> Date {
        let workDate = date ?? now()
        guard let resultDate = calendar.date(from: calendar.dateComponents(components, from: workDate)) else {
            print("⚠️ DateService >>> Can't fetch date.")
            return now()
        }
        return resultDate
    }

    func now() -> Date {
        return Date()
    }

    func thisWeekStart() -> Date {
        return dateWithComponents([.yearForWeekOfYear, .weekOfYear])
    }

    func thisMonthStart() -> Date {
        return dateWithComponents([.year, .month])
    }

    func thisYearStart() -> Date {
        return dateWithComponents([.year])
    }

    func lastWeekStart() -> Date {
        return thisWeekStart() - 1.weeks
    }

    func lastWeekEnd() -> Date {
        return thisWeekStart() - 1.days
    }

    func lastMonthStart() -> Date {
        return dateWithComponents([.year, .month], from: (now() - 1.months))
    }

    func lastMonthEnd() -> Date {
        return thisMonthStart() - 1.days
    }

    func lastYearStart() -> Date {
        return dateWithComponents([.year], from: (now() - 1.years))
    }

    func lastYearEnd() -> Date {
        return thisYearStart() - 1.days
    }