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?
Swift - Get last 7 days starting from today in array
17k views Asked by Bogdan Bogdanov AtThere are 9 answers
It's might not be the fastest code. But you get the idea :)
import Foundation
let lastSevenDay: [Int] = {
var days = [Int]()
let secondsInADay: NSTimeInterval = 24 * 60 * 60
let now = NSDate()
let calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
for i in 1...7 {
let theDate = now.dateByAddingTimeInterval(-secondsInADay * NSTimeInterval(7 - i))
let dateComponent = calendar.components(NSCalendarUnit.CalendarUnitDay, fromDate: theDate)
let dayOfMonth = dateComponent.day
days.append(dayOfMonth)
}
return days
}()
You want to use NSCalendar
to get the last 7 days. Here's a code snippet to demonstrate it in action for 3rd November:
let dateFormatter = NSDateFormatter()
dateFormatter.dateStyle = .ShortStyle
dateFormatter.timeStyle = .NoStyle
dateFormatter.locale = NSLocale(localeIdentifier: "en_GB")
let date = dateFormatter.dateFromString("03/11/14")!
println("date = \(dateFormatter.stringFromDate(date))")
let calendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)!
var previous7days: Array<Int> = []
for i in 0...6 {
let prevDate = calendar.dateByAddingUnit(.CalendarUnitDay, value: -i, toDate: date, options: nil)
previous7days.insert(calendar.component(.CalendarUnitDay, fromDate: prevDate!), atIndex: 0)
}
println("last 7 days: \(previous7days)")
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]
I modified the rintaro answer a bit to get past dates in a NSArray of NSDictionaries to get all values including past days, months and years. You only need to call this snippet like this
println(getPastDates(2))
to get a full list with past dates like that
( { day = 30; month = 11; year = 2014; }, { day = 29; month = 11; year = 2014; } )
func getPastDates(days: Int) -> NSArray {
var dates = NSMutableArray()
let cal = NSCalendar.currentCalendar()
var today = cal.startOfDayForDate(NSDate())
for i in 1 ... days {
let day = cal.component(.DayCalendarUnit, fromDate: today)
let month = cal.component(.MonthCalendarUnit, fromDate: today)
let year = cal.component(.YearCalendarUnit, fromDate: today)
var date = NSMutableDictionary()
date.setValue(day, forKey: "day")
date.setValue(month, forKey: "month")
date.setValue(year, forKey: "year")
dates.addObject(date)
// move back in time by one day:
today = cal.dateByAddingUnit(.DayCalendarUnit, value: -1, toDate: today, options: nil)!
}
return dates
}
I update
Alejandro Luengo's code. It works Swift4
func getPastDates(days: Int) -> NSMutableArray {
let dates = NSMutableArray()
let calendar = Calendar.current
var today = calendar.startOfDay(for: Date())
for _ in 1 ... days {
let day = calendar.component(.day, from: today)
let month = calendar.component(.month, from: today)
let year = calendar.component(.year, from: today)
let date = NSMutableDictionary()
date.setValue(day, forKey: "day")
date.setValue(month, forKey: "month")
date.setValue(year, forKey: "year")
dates.add(date)
today = calendar.date(byAdding: .day, value: -1, to: today)!
}
return dates
}
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"]
Use
NSCalendar
andNSDateComponents
:Update for Swift 2.2 (Xcode 7.3.1):
Update for Swift 3 (Xcode 8 beta 2):