首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在iOS swift中从约会中获取1小时前的信息?

如何在iOS swift中从约会中获取1小时前的信息?
EN

Stack Overflow用户
提问于 2014-12-02 22:19:23
回答 14查看 39.3K关注 0票数 47

我一直在研究,但我找不到解决我的问题的确切方法。我一直在试着从一个小时前的约会中得到。我如何在swift中实现这一点?

EN

回答 14

Stack Overflow用户

回答已采纳

发布于 2014-12-02 22:30:26

对于涉及NSDate的正确计算,考虑到不同日历的所有边缘情况(例如,在夏令时之间切换),您应该使用NSCalendar类:

Swift 3+

代码语言:javascript
复制
let earlyDate = Calendar.current.date(
  byAdding: .hour, 
  value: -1, 
  to: Date())

更老的

代码语言:javascript
复制
// Get the date that was 1hr before now
let earlyDate = NSCalendar.currentCalendar().dateByAddingUnit(
       .Hour,
       value: -1, 
       toDate: NSDate(),
       options: [])
票数 86
EN

Stack Overflow用户

发布于 2015-11-04 15:03:56

使用此方法并粘贴到您的帮助器类中。

针对Swift 3和Xcode 8.3进行了更新

代码语言:javascript
复制
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"
    }
    
}

使用此方法:

代码语言:javascript
复制
var timeAgo:String=AppHelper.timeAgoSinceDate(date, numericDates: true)
Print("\(timeAgo)")   // Ex- 1 hour ago
票数 38
EN

Stack Overflow用户

发布于 2014-12-02 22:23:43

请阅读NSDate类引用。

代码语言:javascript
复制
let oneHourAgo = NSDate.dateWithTimeIntervalSinceNow(-3600)

应该这么做。

或者,对于任何NSDate 对象:

代码语言:javascript
复制
let oneHourBack = myDate.dateByAddingTimeInterval(-3600)

Swift 4:

代码语言:javascript
复制
let oneHourAgo = NSDate(timeIntervalSinceNow: -3600)
票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27251644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档