Ios Developer
Ios Developer

Reputation: 888

To get start and end time of last 7 days

I am trying to get start and end time of last 7 days. start time should be "00:00:00" and end time should be "23:59:59".

  static func getPerticularDates(forLastNDays nDays: Int) -> [Date] {
        var calendar = Calendar.current
           calendar.timeZone = NSTimeZone(abbreviation: "UTC")! as TimeZone
        var dateAtMidnight = calendar.startOfDay(for: Date())
        
           var components = DateComponents()
        var arrDates = [Date]()
        for _ in 0 ... nDays {
            let dateAtEnd = calendar.date(byAdding: components, to: dateAtMidnight)
               // move back in time by one day:
            dateAtMidnight = calendar.date(byAdding: Calendar.Component.day, value: -1, to: dateAtMidnight)!
            print("dateAtMidnight :: \(dateAtMidnight)")
   
               let dateFormatter = DateFormatter()
               dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
               let dateString = dateFormatter.string(from: dateAtMidnight)
               arrDates.append(dateAtMidnight)
           }

        return arrDates

    }

When i am calling following code

let lastSevenDays = Date.getPerticularDates(forLastNDays: 7)
   for i in lastSevenDays{
//            group.enter()
            
            var calendar = Calendar.current
            calendar.timeZone = NSTimeZone(abbreviation: "UTC")! as TimeZone
            let newStartTime = calendar.startOfDay(for: i)
            
            let newEndTime = calendar.date(bySettingHour: 23, minute: 59, second: 59, of: i)
            print("Start Time is \(newStartTime) and end time is \(newEndTime)")
}

My output is

Start Time is 2021-04-15 00:00:00 +0000 and end time is Optional(2021-04-15 23:59:59 +0000)
Start Time is 2021-04-14 00:00:00 +0000 and end time is Optional(2021-04-14 23:59:59 +0000)
Start Time is 2021-04-13 00:00:00 +0000 and end time is Optional(2021-04-13 23:59:59 +0000)
Start Time is 2021-04-12 00:00:00 +0000 and end time is Optional(2021-04-12 23:59:59 +0000)
Start Time is 2021-04-11 00:00:00 +0000 and end time is Optional(2021-04-11 23:59:59 +0000)
Start Time is 2021-04-10 00:00:00 +0000 and end time is Optional(2021-04-10 23:59:59 +0000)
Start Time is 2021-04-09 00:00:00 +0000 and end time is Optional(2021-04-09 23:59:59 +0000)
Start Time is 2021-04-08 00:00:00 +0000 and end time is Optional(2021-04-08 23:59:59 +0000)

But i am not getting todays start time and end time ?

Upvotes: 1

Views: 145

Answers (1)

lpizzinidev
lpizzinidev

Reputation: 13289

You should move back one day after adding the date to the arrDates:

static func getPerticularDates(forLastNDays nDays: Int) -> [Date] {
        var calendar = Calendar.current
       calendar.timeZone = NSTimeZone(abbreviation: "UTC")! as TimeZone
        var dateAtMidnight = calendar.startOfDay(for: Date())
        
        var components = DateComponents()
        var arrDates = [Date]()
        for _ in 1 ... nDays {
            let dateAtEnd = calendar.date(byAdding: components, to: dateAtMidnight)
            print("dateAtMidnight :: \(dateAtMidnight)")
   
            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy/MM/dd HH:mm"
            let dateString = dateFormatter.string(from: dateAtMidnight)
            arrDates.append(dateAtMidnight)

            // MOVE the line below
            // move back in time by one day:
            dateAtMidnight = calendar.date(byAdding: Calendar.Component.day, value: -1, to: dateAtMidnight)!
        }

        return arrDates
}

Upvotes: 2

Related Questions