swift - 如何将日期 "MM-dd-yyyy hh:mm:ss a"转换成文本格式?

标签 swift nsdate

我正在尝试创建一个将日期转换为文本格式的函数,例如“刚刚,2 分钟,1 小时,1 天,10 月 10 日”。这是我在以下位置遇到错误的示例代码:

let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[])

这是我的完整代码:

func getTextToDisplayFormattingDate(date: NSDate) -> String {
    var textToDisplay = ""
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "MM-dd-yyyy hh:mm:ss a"
    dateFormatter.timeZone = TimeZone(identifier: "UTC")

    let cal = NSCalendar.current

    let components = cal.components([.Day,.Hour,.Minute], fromDate: date, toDate: NSDate(), options:[])

    switch components.day {
    case 0:
        if components.hour == 0 {
            if components.minute <= 0 {
                textToDisplay = "just now "
            } else {
                textToDisplay = "\(components.minute) min"
            }
        } else {
            textToDisplay = "\(components.hours) hrs"
        }
    case 1...6:
        textToDisplay = "\(components.day) d"
    default:
        dateFormatter.dateFormat = "MMM dd"
        textToDisplay = dateFormatter.string(from: date as Date)
    }

    return textToDisplay
}

error

最佳答案

像这样使用:

我的代码中的更改:

  1. 代替 NSDate,我使用了 Date
  2. 代替 NSCalendar 我使用 Calendar
  3. components(_:from:to:options:) 已重命名为 dateComponents(_:from:to:)
  4. components 值,例如 day, minute, houroptional。这就是我在切换前添加检查的原因。

    func getTextToDisplayFormattingDate(date: Date) -> String {
        var textToDisplay = ""
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "MM-dd-yyyy hh:mm:ss a"
        dateFormatter.timeZone = TimeZone(identifier: "UTC")
    
        let cal = Calendar.current
        let components = cal.dateComponents([.day, .hour , .minute], from: date, to: Date())
    
        if let day = components.day, let minute = components.minute, let hour = components.hour {
            switch day {
            case 0:
                if hour == 0 {
                    if minute <= 0 {
                        textToDisplay = "just now "
                    } else {
                        textToDisplay = "\(minute) min"
                    }
                } else {
                    textToDisplay = "\(hour) hrs"
                }
            case 1...6:
                textToDisplay = "\(day) d"
            default:
                dateFormatter.dateFormat = "MMM dd"
                textToDisplay = dateFormatter.string(from: date)
            }
        }
        return textToDisplay
    }
    

关于swift - 如何将日期 "MM-dd-yyyy hh:mm:ss a"转换成文本格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46750930/

相关文章:

swift - 对 NSDateFormatter 进行单元测试的好方法是什么?

ios - 如何在 UITextView 中像 block 引号一样格式化文本

regex - 如何在 RegEx 中搜索初始直双引号字符?

objective-c - NSDateFormatter dateFromString 返回不正确的日期

swift - 类 Y 的对象 X 未实现 Swift 中的 methodSignatureForSelector

swift 4.0 : How do i move all the NSArray values to NSDictionary dynamically?

ios - 使用两个字典键基于日期和时间对数组进行排序

ios - 给定年份和月份,如何检查该月是否存在特定日期

ios - 现在在 objective-c iOS 中查找 EST 日期

cocoa-touch - iOS 上的 NSDate 提供人类友好的日期描述