Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Get last 7 days starting from today in array

How to get previous 7 days of the month I know how to get if today is 18, but what if today id 3rd November? How to get the last 4 days from the previous month(October) in Int?

like image 782
Bogdan Bogdanov Avatar asked Nov 18 '14 14:11

Bogdan Bogdanov


Video Answer


3 Answers

Use NSCalendar and NSDateComponents:

let cal = NSCalendar.currentCalendar()
// start with today
var date = cal.startOfDayForDate(NSDate())

var days = [Int]()

for i in 1 ... 7 {
    // get day component:
    let day = cal.component(.DayCalendarUnit, fromDate: date)
    days.append(day)

    // move back in time by one day:
    date = cal.dateByAddingUnit(.DayCalendarUnit, value: -1, toDate: date, options: nil)!
}

println(days)

Update for Swift 2.2 (Xcode 7.3.1):

let cal = NSCalendar.currentCalendar()
var date = cal.startOfDayForDate(NSDate())
var days = [Int]()
for i in 1 ... 7 {
    let day = cal.component(.Day, fromDate: date)
    days.append(day)
    date = cal.dateByAddingUnit(.Day, value: -1, toDate: date, options: [])!
}
print(days)

Update for Swift 3 (Xcode 8 beta 2):

let cal = Calendar.current
var date = cal.startOfDay(for: Date())
var days = [Int]()
for i in 1 ... 7 {
    let day = cal.component(.day, from: date)
    days.append(day)
    date = cal.date(byAdding: .day, value: -1, to: date)!
}
print(days)
like image 188
Martin R Avatar answered Oct 19 '22 00:10

Martin R


Inspired by this answer,

Get list of previous N days as an array of strings.

extension Date {
    static func getDates(forLastNDays nDays: Int) -> [String] {
        let cal = NSCalendar.current
        // start with today
        var date = cal.startOfDay(for: Date())

        var arrDates = [String]()

        for _ in 1 ... nDays {
            // move back in time by one day:
            date = cal.date(byAdding: Calendar.Component.day, value: -1, to: date)!

            let dateFormatter = DateFormatter()
            dateFormatter.dateFormat = "yyyy-MM-dd"
            let dateString = dateFormatter.string(from: date)
            arrDates.append(dateString)
        }
        print(arrDates)
        return arrDates
    }
}

Usage:

let last7Days = Date.getDates(forLastNDays: 7)
debugPrint(last7Days)

//Today(2017-11-19) it prints: ["2017-11-18", "2017-11-17", "2017-11-16", "2017-11-15", "2017-11-14", "2017-11-13", "2017-11-12"]
like image 8
Mohammad Zaid Pathan Avatar answered Oct 19 '22 00:10

Mohammad Zaid Pathan


You can create an extension using Calendar to help you with your calendrical calculations:

Swift 3 or later

extension Date {
    var day: Int {
        return Calendar.current.component(.day, from: self)
    }
    func adding(days: Int) -> Date {
        return Calendar.current.date(byAdding: .day, value: days, to: self)!
    }
    var last7days: [Int] {
        return (1...7).map { 
            adding(days: -$0).day
        }
    }
    func near(days: Int) -> [Int] {
        return days == 0 ? [day] : (1...abs(days)).map {
            adding(days: $0 * (days < 0 ? -1 : 1) ).day
        }
    }
}

usage:

let last7Days  = Date().last7days   // [29, 28, 27, 26, 25, 24, 23]
let last7Days2 = Date().near(days: -7) // [29, 28, 27, 26, 25, 24, 23]
let next7Days  = Date().near(days: 7) // [27, 28, 29, 30, 31, 1, 2]

Same strategy as @rintaro answer, as short as possible ;) (added support to negative values and made it as a Date extension to be able to use any date as input)

extension Date {
    func closest(days: Int) -> [Int] {
        return days == 0 ? [] : (1...abs(days)).map { delta -> Int in Calendar.current.component(.day, from: Calendar.current.date(byAdding: .day, value: delta * (days >= 0 ? 1 : -1), to: self)!) }
    }
}

usage:

let next7Days2 = Date().closest(days: 7) // [27, 28, 29, 30, 31, 1, 2]
let last7Days3 = Date().closest(days: -7) // [25, 24, 23, 22, 21, 20, 19]
let next2Days = Date().closest(days: 2) // [27, 28]
let last2Days = Date().closest(days: -2) // [25, 24]
like image 6
Leo Dabus Avatar answered Oct 19 '22 00:10

Leo Dabus