【问题标题】:Convert Unix timestamp to date object swift 5将 Unix 时间戳转换为日期对象 swift 5
【发布时间】:2021-06-17 09:00:14
【问题描述】:

我需要在 swift 5 编程语言中将 unix 时间戳 转换为 日期对象

Unix 时间戳(以整数表示)

1616187198

print(unixTimeStamp)  //Output: 1616187198
print(Date())         //Output: 2021-03-19 23:30:30 +0000

如何将整数 unix 时间戳转换为与 Date() 对象完全相同。

我唯一的猜测是使用 DateFormatter,但我不确定如何使用 unix 时间戳来解决这个问题

static let dateFormatter: DateFormatter = {
        let dateFormatter = DateFormatter()
        dateFormatter.locale = .current
        dateFormatter.timeZone = .current
        dateFormatter.dateStyle = .medium
        dateFormatter.timeStyle = .short
        return dateFormatter
    }()

【问题讨论】:

    标签: ios swift nsdate nsdateformatter swift5


    【解决方案1】:

    将 unix 时间戳转换为 Double 并调用 Date(timeIntervalSince1970:)

    (注意:这会将时间戳转换为日期。但它在控制台或任何其他形式的字符串输出中如何显示是完全不同的。)

    【讨论】:

      猜你喜欢
      • 2020-10-19
      • 2012-12-09
      • 2020-06-28
      • 2020-12-23
      • 2016-04-24
      • 2012-07-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多