swift - 如何在 ios 中将数据类型声明为时间戳 - swift

标签 swift firebase timestamp google-cloud-firestore

我是iOS编程新手。我正在创建一个与 Firestore 集成的简单程序。在 Firestore 中,我创建了集合,每个文档都包含许多字段。

我在文档中添加了 Timestamp 字段。当我在 xcode 中创建模型时,我如何将我的变量声明为 Timestamp 因为我需要根据 Timestamp 对 tableView 上的数据进行排序

这是 Firestore 中的 Timestamp 的样子:

2018 年 9 月 17 日下午 3:37:43 UTC+7

那么,我如何编写程序并获取当前的Timestamp,就像在Firestore中显示的那样

这是在编程中:

struct RecentCallModel {

var call_answered: Bool?
var call_extension: String?
var call_type: String?
var details: String?
var duration:  String?
var title: String?
// How can i declare as Timestamp
var timestamp: ???

init(call_answered: Bool, call_extension: String, call_type: String, details: String , duration: String, timestamp: String,  title: String) {

    self.call_answered = call_answered
    self.call_extension = call_extension
    self.call_type = call_type
    self.details = details
    self.title = title
 }


}

最佳答案

不熟悉 Firebase。但通常,在 iOS 中,我们将时间戳存储为 TimeInterval,它是 Double 的类型别名。

class func getDateOnly(fromTimeStamp timestamp: TimeInterval) -> String { 
  let dayTimePeriodFormatter = DateFormatter()
  dayTimePeriodFormatter.timeZone = TimeZone.current 
  dayTimePeriodFormatter.dateFormat = "MMMM dd, yyyy - h:mm:ss a z" 
  return dayTimePeriodFormatter.string(from: Date(timeIntervalSince1970: timestamp)) 
} 

关于swift - 如何在 ios 中将数据类型声明为时间戳 - swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367721/

相关文章:

ios - 无法转换 UTC 时间

Firebase 云调度程序功能成本

php - 位于 2 个不同表中的 2 个数字之间的差异的平均值

java - java中获取时间戳

ios - 如何使用 Swift 检查 UITableViewCell 完全可见/加载 - iOS (setSelected)

ios - NSNotification:尝试在其 View 不在窗口层次结构中的 ViewController 上显示 UIAlertController

swift - 无法通过 Facebook 从经过身份验证的用户那里获取电子邮件

android - 应用未运行时 Firebase 不显示通知

mysql - 选择现在和时间戳之间的差异小于 24 小时

swift - 从字符串中删除标点符号,但 Swift 中的破折号除外