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

时间:2016-08-01 14:11:53

标签: swift nsdate nsdateformatter nscalendar

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

        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'是今天然后做点什么? 那么你如何本地化时间以使其对用户而不是服务器时间更正?

2 个答案:

答案 0 :(得分:6)

NSCalendar上有一个方便的功能,告诉你NSDate是否在今天(至少需要iOS 8)isDateInToday()

要看到它正常工作,请将其放入游乐场:

// 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)

或者,如果您只是想在不自行运行的情况下看到它的样子:

enter image description here

答案 1 :(得分:0)

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)