Swift 根据时间戳显示时间或日期

Swift displaying the time or date based on timestamp

我有一个 API return 的数据,包括该记录的时间戳。 在 swift 中,我加载了时间戳元素并将其转换为双精度,然后可以将其转换为时间。如果记录日期是今天,我希望能够 return 时间,如果记录不是今天,我希望能够 return 日期。 见下文:

        let unixTimeString:Double = Double(rowData["Timestamp"] as! String)!
        let date = NSDate(timeIntervalSince1970: unixTimeString) // This is on EST time and has not yet been localised.
        var dateFormatter = NSDateFormatter()
        dateFormatter.timeStyle = .ShortStyle
        dateFormatter.doesRelativeDateFormatting = true
        // If the date is today then just display the time, if the date is not today display the date and change the text color to grey.
        var stringTimestampResponse = dateFormatter.stringFromDate(date)
        cell.timestampLabel.text = String(stringTimestampResponse)

我是否使用 NSCalendar 查看 'date' 是否是今天,然后再做些什么? 那么您如何本地化时间以使其对用户而不是服务器时间正确?

NSCalendar 上有一个方便的函数,可以告诉您 NSDate 是否在今天(至少需要 iOS 8)isDateInToday()

要查看它是否正常工作,请将它放到 playground 中:

// Create a couple of unix dates.
let timeIntervalToday: NSTimeInterval = NSDate().timeIntervalSince1970
let timeIntervalLastYear: NSTimeInterval = 1438435830


// This is just to show what the dates are.
let now = NSDate(timeIntervalSince1970: timeIntervalToday)
let then = NSDate(timeIntervalSince1970: timeIntervalLastYear)

// This is the function to show a formatted date from the timestamp
func displayTimestamp(ts: Double) -> String {
    let date = NSDate(timeIntervalSince1970: ts)
    let formatter = NSDateFormatter()
    formatter.timeZone = NSTimeZone.systemTimeZone()

    if NSCalendar.currentCalendar().isDateInToday(date) {
        formatter.dateStyle = .NoStyle
        formatter.timeStyle = .ShortStyle
    } else {
        formatter.dateStyle = .ShortStyle
        formatter.timeStyle = .NoStyle
    }

    return formatter.stringFromDate(date)
}

// This should just show the time.
displayTimestamp(timeIntervalToday)

// This should just show the date.
displayTimestamp(timeIntervalLastYear)

或者,如果您只是想亲自看看没有 运行 它的样子:

Abizern 在 Swift 3:

中的回答
import UIKit
let timeIntervalToday: TimeInterval = Date().timeIntervalSince1970
let timeIntervalLastYear: TimeInterval = 1438435830

// This is just to show what the dates are.
let now = Date(timeIntervalSince1970: timeIntervalToday)
let then = Date(timeIntervalSince1970: timeIntervalLastYear)

// This is the function to show a formatted date from the timestamp
func displayTimestamp(ts: Double) -> String {
    let date = Date(timeIntervalSince1970: ts)
    let formatter = DateFormatter()
    //formatter.timeZone = NSTimeZone.system

    if Calendar.current.isDateInToday(date) {
        formatter.dateStyle = .none
        formatter.timeStyle = .short
    } else {
        formatter.dateStyle = .short
        formatter.timeStyle = .none
    }

    return formatter.string(from: date)
}

// This should just show the time.
displayTimestamp(ts: timeIntervalToday)

// This should just show the date.
displayTimestamp(ts: timeIntervalLastYear)