如何从iOS中快速的1个小时前获取?
我一直在研究,但我无法find我的问题的确切解决scheme。 我一直试图从1个小时前的date。 我怎样才能快速实现这一点? 对不起,我是新来的ios。
谢谢你的回答,最好的问候
为了正确的计算涉及到NSDate考虑到所有不同日历的边缘情况(例如,在节省时间之间切换),您应该使用NSCalendar类:
// Get the date that was 1hr before now let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit( .Hour, value: -1, toDate: NSDate(), options: [])
使用这个方法并粘贴你的助手类。
Swift 3和XCode 8.3的更新
class func timeAgoSinceDate(_ date:Date,currentDate:Date, numericDates:Bool) -> String { let calendar = Calendar.current let now = currentDate let earliest = (now as NSDate).earlierDate(date) let latest = (earliest == now) ? date : 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()) if (components.year! >= 2) { return "\(components.year!) years ago" } else if (components.year! >= 1){ if (numericDates){ return "1 year ago" } else { return "Last year" } } else if (components.month! >= 2) { return "\(components.month!) months ago" } else if (components.month! >= 1){ if (numericDates){ return "1 month ago" } else { return "Last month" } } else if (components.weekOfYear! >= 2) { return "\(components.weekOfYear!) weeks ago" } else if (components.weekOfYear! >= 1){ if (numericDates){ return "1 week ago" } else { return "Last week" } } else if (components.day! >= 2) { return "\(components.day!) days ago" } else if (components.day! >= 1){ if (numericDates){ return "1 day ago" } else { return "Yesterday" } } else if (components.hour! >= 2) { return "\(components.hour!) hours ago" } else if (components.hour! >= 1){ if (numericDates){ return "1 hour ago" } else { return "An hour ago" } } else if (components.minute! >= 2) { return "\(components.minute!) minutes ago" } else if (components.minute! >= 1){ if (numericDates){ return "1 minute ago" } else { return "A minute ago" } } else if (components.second! >= 3) { return "\(components.second!) seconds ago" } else { return "Just now" } }
使用这种方法:
var timeAgo:String=AppHelper.timeAgoSinceDate(date, numericDates: true) Print("\(timeAgo)") // Ex- 1 hour ago
请阅读NSDate
类的参考。
let oneHourAgo = NSDate.dateWithTimeIntervalSinceNow(-3600)
应该这样做。
或者,对于任何NSDate
对象:
let oneHourBack = myDate.dateByAddingTimeInterval(-3600)
Swift3:
let now = Date() let tempCalendar = Calendar.current let alteredDate = tempCalendar.date(byAdding: .hour, value: -1, to: now)
根据你的需要,你可以select3个以下的Swift 3方法之一从Date
实例中获取一个小时前的内容。
1. date(byAdding:value:to:wrappingComponents:)
Calendar
有一个称为date(byAdding:value:to:wrappingComponents:)
的方法date(byAdding:value:to:wrappingComponents:)
。 date(byAdding:value:to:wrappingComponents:)
具有以下声明:
func date(byAdding component: Calendar.Component, value: Int, to date: Date, wrappingComponents: Bool = default) -> Date?
返回一个新的
Date
表示通过向给定date添加一定数量的特定组件来计算的date。
下面的Playground代码展示了如何使用它:
import Foundation let now = Date() let oneHourAgo = Calendar.current.date(byAdding: .hour, value: -1, to: now) print(now) // 2016-12-19 21:52:04 +0000 print(String(describing: oneHourAgo)) // Optional(2016-12-19 20:52:04 +0000)
2. date(byAdding:to:wrappingComponents:)
Calendar
有一个叫date(byAdding:to:wrappingComponents:)
的方法date(byAdding:to:wrappingComponents:)
。 date(byAdding:value:to:wrappingComponents:)
具有以下声明:
func date(byAdding components: DateComponents, to date: Date, wrappingComponents: Bool = default) -> Date?
返回一个新的
Date
表示通过向给定date添加组件来计算的date。
下面的Playground代码展示了如何使用它:
import Foundation let now = Date() var components = DateComponents() components.hour = -1 let oneHourAgo = Calendar.current.date(byAdding: components, to: now) print(now) // 2016-12-19 21:52:04 +0000 print(String(describing: oneHourAgo)) // Optional(2016-12-19 20:52:04 +0000)
替代scheme:
import Foundation // Get the date that was 1hr before now let now = Date() let components = DateComponents(hour: -1) let oneHourAgo = Calendar.current.date(byAdding: components, to: now) print(now) // 2016-12-19 21:52:04 +0000 print(String(describing: oneHourAgo)) // Optional(2016-12-19 20:52:04 +0000)
3. addingTimeInterval(_:)
(谨慎使用)
Date
有一个方法称为addingTimeInterval(_:)
。 addingTimeInterval(_:)
具有以下声明:
func addingTimeInterval(_ timeInterval: TimeInterval) -> Date
通过向此
Date
添加TimeInterval
来返回新的Date
。
请注意,此方法带有警告:
这只调整一个绝对值。 如果您希望添加小时,天,月等日历概念,则必须使用
Calendar
。 这将考虑夏令时,每月不同天数等复杂性。
下面的Playground代码展示了如何使用它:
import Foundation let now = Date() let oneHourAgo = now.addingTimeInterval(-3600) print(now) // 2016-12-19 21:52:04 +0000 print(oneHourAgo) // 2016-12-19 20:52:04 +0000
如果你正在使用NSDate
你可以这样做:
let date = NSDate() date.dateByAddingTimeInterval(-3600)
它会将date
对象更改为“1小时前”。
对于Swift 2:
extension NSDate { func after(value: Int, calendarUnit:NSCalendarUnit) -> NSDate { return calendar.dateByAddingUnit(calendarUnit, value: value, toDate: self, options: [])! } }
如何使用:
let lastHour = NSDate().after(-1, calendarUnit: .Hour)
我通过创buildDate的扩展来实现前一个function。 下面给出了:
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 "" } }
然后我通过计算当前date和指定date之间的差异来得到:
let timeAgo = Date().timeAgo(from: sender.date)