我是一个新的iOS编程。我正在创建一个与Firestore
集成的简单程序。在Firestore
中,我创建了集合,每个文档都包含许多字段。
我在文档中添加了Timestamp
字段。当我在xcode
中创建模型时,我如何将变量声明为Timestamp
,因为我需要基于Timestamp
在tableView
上对数据进行排序。
这就是Timestamp
在Firestore
中的样子:
September 17, 2018 at 3:37:43 PM 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
}
}
发布于 2018-09-17 04:30:16
不熟悉火柴基地。但是通常,在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))
}
发布于 2018-09-17 04:50:26
这是我们如何快速获得当前日期和时间的方法。您也可以将时间戳声明为Double
,但我不确定
// get the current date and time
let currentDateTime = Date()
// initialize the date formatter and set the style
let formatter = DateFormatter()
formatter.timeStyle = .medium
formatter.dateStyle = .long
// get the date time String from the date object
formatter.string(from: currentDateTime)
发布于 2018-09-17 04:43:07
不确定您的问题显式地暗示了什么,但是您通常将时间戳值声明为Double
,类似于var timestamp: Double
。检索到的数据将类似于下面的1537187800,可以使用下面的帮助类将其转换为实际日期和/或时间
class DateAndTimeHelper {
static func convert(timestamp: Double, toDateFormat dateFormat: String) -> String {
let date = Date(timeIntervalSince1970: timestamp)
let dateFormatter = DateFormatter()
dateFormatter.timeZone = NSTimeZone.local
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = dateFormat
return dateFormatter.string(from: date)
}
}
可以使用以下语法使用
var timestamp: Double = 0.0
// Perform some database querying action in order to retrieve the actual timestamp value from the server
DateAndTimeHelper.convert(timestamp: timestamp, toDateFormat: DATE_FORMAT)
请注意,您应该将DATE_FORMAT替换为您希望遵循的格式。这里可以很好地参考可用的格式。
https://stackoverflow.com/questions/52367721
复制相似问题