Skip to content

Instantly share code, notes, and snippets.

@arslanbilal
Last active August 29, 2015 14:20
Show Gist options
  • Save arslanbilal/7154ae11c888fb0dd154 to your computer and use it in GitHub Desktop.
Save arslanbilal/7154ae11c888fb0dd154 to your computer and use it in GitHub Desktop.
Creating "today's date and last seven days' date" in to the String array in Swift Lang.
let date = NSDate() // Today's date.
let dateFormatter = NSDateFormatter() // Creating a dateFormatter object for making readable date
var array = [String]() // Store the today's date and last 7 date.
let secondInADay: NSTimeInterval = 24 * 60 * 60 // A day NSTimeInterval
let timeInterval = date.timeIntervalSince1970 // Today's NSTimeInteval
dateFormatter.dateFormat = "EE-yyyyMMdd" // Making a dateFormat
dateFormatter.locale = NSLocale(localeIdentifier:"us") //
array.append(dateFormatter.stringFromDate(date)) // Adding today's date in to the array
for i in 1 ... 7 { // Going back 7 days loops.
let newDate = NSDate(timeIntervalSince1970: timeInterval - (secondInADay * NSTimeInterval(i))) // going one day back
array.append(dateFormatter.stringFromDate(newDate)) // Adding the current newDate in to the array
}
println(array)
// [Thu-20150507, Wed-20150506, Tue-20150505, Mon-20150504, Sun-20150503, Sat-20150502, Fri-20150501, Thu-20150430]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment