get the date 5 days ago in swift

Here's a code snippet that gets the date 5 days ago in Swift:

main.swift
let calendar = Calendar.current
let today = Date()
let fiveDaysAgo = calendar.date(byAdding: .day, value: -5, to: today)
121 chars
4 lines

The code above gets the current date using Date(), and then subtracts 5 days from it using calendar.date(byAdding:to:). The value parameter is set to -5 to subtract days, and the to parameter is set to today to subtract from the current date. The resulting date is stored in the fiveDaysAgo constant.

gistlibby LogSnag