Vla*_*mir 54
对于涉及NSDate的正确计算,考虑到不同日历的所有边缘情况(例如,在节省时间之间切换),您应该使用NSCalendar类:
Swift 3+
let earlyDate = Calendar.current.date(
byAdding: .hour,
value: -1,
to: Date())
Run Code Online (Sandbox Code Playgroud)
年长
// Get the date that was 1hr before now
let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit(
.Hour,
value: -1,
toDate: NSDate(),
options: [])
Run Code Online (Sandbox Code Playgroud)
Sou*_*rma 34
使用此方法并粘贴到助手类中.
更新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"
}
}
Run Code Online (Sandbox Code Playgroud)
使用此方法:
var timeAgo:String=AppHelper.timeAgoSinceDate(date, numericDates: true)
Print("\(timeAgo)") // Ex- 1 hour ago
Run Code Online (Sandbox Code Playgroud)
zis*_*oft 10
请阅读NSDate课程参考.
let oneHourAgo = NSDate.dateWithTimeIntervalSinceNow(-3600)
Run Code Online (Sandbox Code Playgroud)
应该这样做.
或者,对于任何NSDate对象:
let oneHourBack = myDate.dateByAddingTimeInterval(-3600)
Run Code Online (Sandbox Code Playgroud)
根据您的需要,您可以从一个Date实例中选择以下3个Swift 3方法中的一个来获取一个小时.
date(byAdding:value:to:wrappingComponents:)Calendar有一个叫做的方法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?
Run Code Online (Sandbox Code Playgroud)
返回
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)
Run Code Online (Sandbox Code Playgroud)
date(byAdding:to:wrappingComponents:)Calendar有一个叫做的方法date(byAdding:to:wrappingComponents:).date(byAdding:value:to:wrappingComponents:)有以下声明:
func date(byAdding components: DateComponents, to date: Date, wrappingComponents: Bool = default) -> Date?
Run Code Online (Sandbox Code Playgroud)
返回
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)
Run Code Online (Sandbox Code Playgroud)
替代方案:
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)
Run Code Online (Sandbox Code Playgroud)
addingTimeInterval(_:)(谨慎使用)Date有一个叫做的方法addingTimeInterval(_:).addingTimeInterval(_:)有以下声明:
func addingTimeInterval(_ timeInterval: TimeInterval) -> Date
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您正在使用NSDate,则可以执行以下操作:
let date = NSDate()
date.dateByAddingTimeInterval(-3600)
Run Code Online (Sandbox Code Playgroud)
它将date对象更改为“ 1小时前”。
斯威夫特3:
let now = Date()
let tempCalendar = Calendar.current
let alteredDate = tempCalendar.date(byAdding: .hour, value: -1, to: now)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24970 次 |
| 最近记录: |