使用各种格式Swift iOS将时间戳转换为日期

时间:2018-01-18 11:45:38

标签: ios json date datetime swift3

代码: -

let addedTime = dictMessage?.object(forKey: "added_time") as? String
let timeinterval : TimeInterval = (addedTime as NSString).doubleValu                
let dateFromServer = NSDate(timeIntervalSince1970:timeinterval)
let dateFormater : DateFormatter = DateFormatter()              
dateFormater.dateFormat = "dd-MMM-yyyy hh:mm a"
cellDetailsShop.lblTimeShop?.text = dateFormater.string(from: dateFromServer as Date)

我有一个json并且从json获得Timestamp我已经转换为Date.Date,我收到它的格式为"dd-MMM-yyyy hh:mm a"。但我想显示日期,如果日期今天它将显示:Today 04:55 PM,如果日期是昨天,那么我将显示如下:Yesterday 5 PM如果在此之前那么它应该显示为我从JSON检索。我搜索了各种链接但找不到答案。有人可以帮我。谢谢。

4 个答案:

答案 0 :(得分:1)

从日历对象开始,如果日期是今天或昨天,您可以获得。 基于此,您可以根据需要设置String。 我已经创建了一个你需要的例子。 下面是代码。

var yesterdayStr = "17-Jan-2018 10:23 AM"
var todayStr = "18-Jan-2018 10:23 AM"
var tomorrowStr = "19-Jan-2018 10:23 AM"
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MMM-yyyy hh:mm a"
let yesterDaydate = dateFormatter.date(from: yesterdayStr)
let todayDate = dateFormatter.date(from: todayStr)
let tomorrowDate = dateFormatter.date(from: tomorrowStr)
let anotherDateFormatter = DateFormatter()
anotherDateFormatter.dateFormat = "hh:mm a"
if Calendar.current.isDateInYesterday(yesterDaydate!)
{
    let yesterdayDisplayString = "Yesterday, " + anotherDateFormatter.string(from: yesterDaydate!)
}
if Calendar.current.isDateInToday(todayDate!) {
    let todayDisplayString = "Today, " + anotherDateFormatter.string(from: todayDate!)
}
if Calendar.current.isDateInToday(tomorrowDate!) {

}
else {
    let anotherDisplayString = anotherDateFormatter.string(from: tomorrowDate!)
}

这是输出, github

答案 1 :(得分:1)

你可以这样做。

let addedTime = dictMessage?.object(forKey: "added_time") as? String
let timeinterval : TimeInterval = (addedTime as NSString).doubleValue
let dateFromServer = NSDate(timeIntervalSince1970:timeinterval)
let dateFormater : DateFormatter = DateFormatter()
if Calendar.current.isDateInToday(dateFromServer) {
    dateFormater.dateFormat = "'Today' hh:mm a"
}
else if Calendar.current.isDateInYesterday(dateFromServer) {
    dateFormater.dateFormat = "'Yesterday' hh:mm a"
}
else {
    dateFormater.dateFormat = "dd-MMM-yyyy hh:mm a"
}
cellDetailsShop.lblTimeShop?.text = dateFormater.string(from: dateFromServer as Date)

如果您有任何意见,请与我们联系。

感谢。

答案 2 :(得分:0)

使用以下代码

   func relativeDate(for date:Date) -> String {
        let components = Calendar.current.dateComponents([.day, .year, .month, .weekOfYear], from: date, to: Date())
        if let year = components.year, year == 1{
            return "\(year) year ago"
        }
        if let year = components.year, year > 1{
            return "\(year) years ago"
        }
        if let month = components.month, month == 1{
            return "\(month) month ago"
        }
        if let month = components.month, month > 1{
            return "\(month) months ago"
        }

        if let week = components.weekOfYear, week == 1{
            return "\(week) week ago"
        }
        if let week = components.weekOfYear, week > 1{
            return "\(week) weeks ago"
        }

        if let day = components.day{
            if day > 1{
                return "\(day) days ago"
            }else{
                "Yesterday"
            }
        }
        return "Today"
    }

答案 3 :(得分:0)

如果您可以在没有自定义日期格式的情况下执行此操作DateFormatter提供本地化的相对格式。日期格式考虑系统偏好设置中的设置。

let dateFromServer = Date(timeIntervalSinceNow: -100000)
let dateFormatter = DateFormatter()
dateFormatter.doesRelativeDateFormatting = true
dateFormatter.timeStyle = .short
dateFormatter.dateStyle = .short
let string = dateFormatter.string(from: dateFromServer)

请不要在Swift中使用基金会NSStringNSDate,使用原生结构

if let addedTime = dictMessage?["added_time"] as? String,
    let timeinterval = TimeInterval(addedTime) {
        let dateFromServer = Date(timeIntervalSince1970:timeinterval)
        let dateFormater = DateFormatter() 
        ...
}