Naresh
Naresh

Reputation: 17902

Get last week and last month start and end dates

Get last week and last month start and end dates.

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"
let date = Date()

//Today date
let dateString = dateFormatter.string(from: date)
print(dateString)

//Yesterday
let yesterday = Calendar.current.date(byAdding: .day, value: -1, to: date)!
print(dateFormatter.string(from: yesterday))

//This week
let startWeek = date.startOfWeek
let endWeek = date.endOfWeek
print(startWeek ?? "not found start date")
print(endWeek ?? "not found end date")

extension Date {//Get this week starts from monday
var startOfWeek: Date? {
    let gregorian = Calendar(identifier: .gregorian)
    guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
    return gregorian.date(byAdding: .day, value: 2, to: sunday)
}

var endOfWeek: Date? {
    let gregorian = Calendar(identifier: .gregorian)
    guard let sunday = gregorian.date(from: gregorian.dateComponents([.yearForWeekOfYear, .weekOfYear], from: self)) else { return nil }
    return gregorian.date(byAdding: .day, value: 8, to: sunday)
}

}

//This month
dateFormatter.dateFormat = "MM-yyyy"
let nameOfMonth = dateFormatter.string(from: date)
print(nameOfMonth)

//Last 30days
let last30Days = Calendar.current.date(byAdding: .day, value: -30, to: date)!
print(dateFormatter.string(from: last30Days))

I need last week start date and end date[Start day is Monday and end day is sunday]

And last month start date and end date

Upvotes: 0

Views: 631

Answers (1)

LorenzOliveto
LorenzOliveto

Reputation: 7936

You should use this function to calculate the range of dates, it takes a target date, a calendar component and two inout parameters that will be populated with the date and time interval calculated.

func dateInterval(of component: Calendar.Component, start: inout Date, interval: inout TimeInterval, for date: Date) -> Bool

Here's an example:

let calendar = Calendar.current
let today = Date()

let lastWeek = calendar.date(byAdding: .weekOfYear, value: -1, to: today)
let lastMonth = calendar.date(byAdding: .month, value: -1, to: today)

if let lastWeek = lastWeek {
    var startOfLastWeek = Date()
    var interval = TimeInterval(0)

    calendar.dateInterval(of: .weekOfYear, start: &startOfLastWeek, interval: &interval, for: lastWeek)

    let endOfLastWeek = startOfLastWeek.addingTimeInterval(interval)

    print(startOfLastWeek)
    print(endOfLastWeek)
}

if let lastMonth = lastMonth {
    var startOfLastMonth = Date()
    var interval = TimeInterval(0)

    calendar.dateInterval(of: .month, start: &startOfLastMonth, interval: &interval, for: lastMonth)

    let endOfLastMonth = startOfLastMonth.addingTimeInterval(interval)

    print(startOfLastMonth)
    print(endOfLastMonth)
}

If you need to change the first day of the week you can change it in your calendar like this:

var calendar = Calendar.current
calendar.firstWeekday = 2 // 1 is Sunday, 2 is Monday

Remember that when you print the date print(startOfLastMonth), it is displayed in UTC, if you want the date printed in your timezone you will have to use a DateFormatter.

Upvotes: 1

Related Questions