I have been researching, but I couldnt find exact solution for my problem. I have been trying to get 1 hour ago from a date. How can I achieve this in swift?
This works for iOS 13 / Swift 5. Credit goes to Sourabh Sharma
func timeAgoSinceNow(numericDates: Bool = true) -> String {
let calendar = Calendar.current
let now = Date()
let earliest = (now as NSDate).earlierDate(self)
let latest = (earliest == now) ? self : now
let components: DateComponents = (calendar as NSCalendar).components([NSCalendar.Unit.minute,
NSCalendar.Unit.hour,
NSCalendar.Unit.day,
NSCalendar.Unit.weekOfYear,
NSCalendar.Unit.month,
NSCalendar.Unit.year,
NSCalendar.Unit.second],
from: earliest,
to: latest,
options: NSCalendar.Options())
guard
let year = components.year,
let month = components.month,
let weekOfYear = components.weekOfYear,
let day = components.day,
let hour = components.hour,
let minute = components.minute,
let second = components.second
else { return "A while ago"}
if year >= 1 {
return year >= 2 ? "\(year) years ago" : numericDates ? "1 year ago" : "Last year"
} else if month >= 1 {
return month >= 2 ? "\(month) months ago" : numericDates ? "1 month ago" : "Last month"
} else if weekOfYear >= 1 {
return weekOfYear >= 2 ? "\(weekOfYear) weeks ago" : numericDates ? "1 week ago" : "Last week"
} else if day >= 1 {
return day >= 2 ? "\(day) days ago" : numericDates ? "1 day ago" : "Yesterday"
} else if hour >= 1 {
return hour >= 2 ? "\(hour) hours ago" : numericDates ? "1 hour ago" : "An hour ago"
} else if minute >= 1 {
return minute >= 2 ? "\(minute) minutes ago" : numericDates ? "1 minute ago" : "A minute ago"
} else {
return second >= 3 ? "\(second) seconds ago" : "Just now"
}
}
Usage:
var date = Date() // Or any date you wish to convert to text
print("\(date.timeAgoSinceNow())") // "Just Now"
Please read the NSDate
class reference.
let oneHourAgo = NSDate.dateWithTimeIntervalSinceNow(-3600)
should do it.
Or, for any NSDate
object:
let oneHourBack = myDate.dateByAddingTimeInterval(-3600)
Swift 4:
let oneHourAgo = NSDate(timeIntervalSinceNow: -3600)
I achieve time ago functionality by creating extension of Date. Which is given below:
extension Date {
// Returns the number of years
func yearsCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.year], from: date, to: self).year ?? 0
}
// Returns the number of months
func monthsCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.month], from: date, to: self).month ?? 0
}
// Returns the number of weeks
func weeksCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.weekOfMonth], from: date, to: self).weekOfMonth ?? 0
}
// Returns the number of days
func daysCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.day], from: date, to: self).day ?? 0
}
// Returns the number of hours
func hoursCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.hour], from: date, to: self).hour ?? 0
}
// Returns the number of minutes
func minutesCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.minute], from: date, to: self).minute ?? 0
}
// Returns the number of seconds
func secondsCount(from date: Date) -> Int {
return Calendar.current.dateComponents([.second], from: date, to: self).second ?? 0
}
// Returns time ago by checking if the time differences between two dates are in year or months or weeks or days or hours or minutes or seconds
func timeAgo(from date: Date) -> String {
if yearsCount(from: date) > 0 { return "\(yearsCount(from: date))years ago" }
if monthsCount(from: date) > 0 { return "\(monthsCount(from: date))months ago" }
if weeksCount(from: date) > 0 { return "\(weeksCount(from: date))weeks ago" }
if daysCount(from: date) > 0 { return "\(daysCount(from: date))days ago" }
if hoursCount(from: date) > 0 { return "\(hoursCount(from: date))hours ago" }
if minutesCount(from: date) > 0 { return "\(minutesCount(from: date))minutes ago" }
if secondsCount(from: date) > 0 { return "\(secondsCount(from: date))seconds ago" }
return ""
}
}
Then i get the time ago by calculating the difference between current date and specified date:
let timeAgo = Date().timeAgo(from: sender.date)
You can also use an operator
let date = Date()
let anHourAgo = date - TimeInterval(3600.0)
Apple Docs: https://developer.apple.com/documentation/foundation/date/2293436
For correct calculations involving NSDate that take into account all edge cases of different calendars (e.g. switching between day saving time) you should use NSCalendar class:
Swift 3+
let earlyDate = Calendar.current.date(
byAdding: .hour,
value: -1,
to: Date())
Older
// Get the date that was 1hr before now
let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit(
.Hour,
value: -1,
toDate: NSDate(),
options: [])
for Swift 2:
extension NSDate {
func after(value: Int, calendarUnit:NSCalendarUnit) -> NSDate {
return calendar.dateByAddingUnit(calendarUnit, value: value, toDate: self, options: [])!
}
}
how to use:
let lastHour = NSDate().after(-1, calendarUnit: .Hour)