如何将数据作为时间戳数据类型发布到Firestore-swift
我想知道如何将数据作为如何将数据作为时间戳数据类型发布到Firestore-swift,swift,firebase,timestamp,google-cloud-firestore,Swift,Firebase,Timestamp,Google Cloud Firestore,我想知道如何将数据作为String发布到Firestore作为Timestamp。我正在创建一个示例应用程序,它将数据存储在tableView上,数据将根据Timestamp进行排序 因此,在编程中,我试图将当前时间和日期设置为String,但我不知道如何将数据设置为Firestore,因为字段是Timestamp。当我从Firestore查询数据时,我想根据Timestamp对数据库进行排序 let currentDateTime = Date() // initialize the dat
String
发布到Firestore
作为Timestamp
。我正在创建一个示例应用程序,它将数据存储在tableView
上,数据将根据Timestamp
进行排序
因此,在编程中,我试图将当前时间和日期设置为String
,但我不知道如何将数据设置为Firestore
,因为字段是Timestamp
。当我从Firestore
查询数据时,我想根据Timestamp
对数据库进行排序
let currentDateTime = Date()
// initialize the date formatter and set the style
let formatter = DateFormatter()
formatter.timeStyle = .long
formatter.dateStyle = .long
// get the date time String from the date object
formatter.string(from: currentDateTime)
Firestore
中的Timestamp
包含日期和时间。
如何将数据作为
String
发布到Firestore
作为Timestamp
?如果我理解正确,您希望在Firestore中以时间戳或日期存储文件
你似乎把时间戳字面上理解为日期,一种更简单的方法是将它存储为时间戳,因为它只是一个将在Firestore中订购的数字
let timestamp = Int(NSDate.timeIntervalSinceReferenceDate*1000).description
如果我理解正确,您想在Firestore中存储带有时间戳或日期的文件吗 你似乎把时间戳字面上理解为日期,一种更简单的方法是将它存储为时间戳,因为它只是一个将在Firestore中订购的数字
let timestamp = Int(NSDate.timeIntervalSinceReferenceDate*1000).description
为什么要将其作为
字符串发布?以下是我使用Int
的方法
let timestamp=Int(Date().timeIntervalSince1970)//提供一个类似于1534840591的Int
然后,当您从Firebase
解析它时,将其传递给func
,类似这样的内容,以将Int
时间戳转换为日期:
func timestampIntToString(integerTime: Int, timestampLabel: UILabel) {
let timestampDate = Date(timeIntervalSince1970: Double(integerTime))
let now = Date()
let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth])
let difference = Calendar.current.dateComponents(components, from: timestampDate, to: now)
var timeText = ""
if difference.second! <= 0 {
timeText = "now"
}
if difference.second! > 0 && difference.minute! == 0 {
timeText = "\(difference.second!) sec ago"
}
if difference.minute! > 0 && difference.hour! == 0 {
timeText = "\(difference.minute!) min"
}
if difference.hour! > 0 && difference.day! == 0 {
timeText = "\(difference.hour!)h"
}
if difference.day! > 0 && difference.weekOfMonth! == 0 {
timeText = (difference.day == 1) ? "\(difference.day!)day" : "\(difference.day!) days ago"
}
if difference.weekOfMonth! > 0 {
timeText = (difference.weekOfMonth == 1) ? "\(difference.weekOfMonth!) w" : "\(difference.weekOfMonth!)w"
}
timestampLabel.text = timeText
}
func timestampIntToString(整数时间:Int,timestampLabel:UILabel){
设timestampDate=Date(timeIntervalSince1970:Double(整数))
让我们现在=日期()
let components=Set([.second、.minute、.hour、.day、.weekOfMonth])
让差异=Calendar.current.dateComponents(组件,从:timestampDate,到:now)
var timeText=“”
如果difference.second!0&&difference.minute!==0{
timeText=“\(差秒!)秒前”
}
如果difference.minute!>0&&difference.hour!==0{
timeText=“\(差异.分钟!)分钟”
}
如果difference.hour!>0&&difference.day!==0{
timeText=“\(差异.小时!)h”
}
如果difference.day!>0&&difference.weekOfMonth!==0{
timeText=(difference.day==1)?“\(difference.day!)day”:“\(difference.day!)天前”
}
如果差异.weekOfMonth!>0{
timeText=(difference.weekOfMonth==1)?“\(difference.weekOfMonth!)w”:“\(difference.weekOfMonth!)w”
}
timestampLabel.text=timeText
}
为什么要将其作为字符串发布?以下是我使用Int
的方法
let timestamp=Int(Date().timeIntervalSince1970)//提供一个类似于1534840591的Int
然后,当您从Firebase
解析它时,将其传递给func
,类似这样的内容,以将Int
时间戳转换为日期:
func timestampIntToString(integerTime: Int, timestampLabel: UILabel) {
let timestampDate = Date(timeIntervalSince1970: Double(integerTime))
let now = Date()
let components = Set<Calendar.Component>([.second, .minute, .hour, .day, .weekOfMonth])
let difference = Calendar.current.dateComponents(components, from: timestampDate, to: now)
var timeText = ""
if difference.second! <= 0 {
timeText = "now"
}
if difference.second! > 0 && difference.minute! == 0 {
timeText = "\(difference.second!) sec ago"
}
if difference.minute! > 0 && difference.hour! == 0 {
timeText = "\(difference.minute!) min"
}
if difference.hour! > 0 && difference.day! == 0 {
timeText = "\(difference.hour!)h"
}
if difference.day! > 0 && difference.weekOfMonth! == 0 {
timeText = (difference.day == 1) ? "\(difference.day!)day" : "\(difference.day!) days ago"
}
if difference.weekOfMonth! > 0 {
timeText = (difference.weekOfMonth == 1) ? "\(difference.weekOfMonth!) w" : "\(difference.weekOfMonth!)w"
}
timestampLabel.text = timeText
}
func timestampIntToString(整数时间:Int,timestampLabel:UILabel){
设timestampDate=Date(timeIntervalSince1970:Double(整数))
让我们现在=日期()
let components=Set([.second、.minute、.hour、.day、.weekOfMonth])
让差异=Calendar.current.dateComponents(组件,从:timestampDate,到:now)
var timeText=“”
如果difference.second!0&&difference.minute!==0{
timeText=“\(差秒!)秒前”
}
如果difference.minute!>0&&difference.hour!==0{
timeText=“\(差异.分钟!)分钟”
}
如果difference.hour!>0&&difference.day!==0{
timeText=“\(差异.小时!)h”
}
如果difference.day!>0&&difference.weekOfMonth!==0{
timeText=(difference.day==1)?“\(difference.day!)day”:“\(difference.day!)天前”
}
如果差异.weekOfMonth!>0{
timeText=(difference.weekOfMonth==1)?“\(difference.weekOfMonth!)w”:“\(difference.weekOfMonth!)w”
}
timestampLabel.text=timeText
}
我发布为字符串,因为当我获得当前时间和日期时,结果将如下2018年9月17日格林尼治时间晚上8:28:29+7
。然后我想将它存储在Firestore
中,稍后,当我查询它时,我可以根据当前日期和时间对它进行排序。例如,recent call it根据当前日期和时间在tableView上存储数据。我想实现这样的目标。我以字符串的形式发布,因为当我获得当前时间和日期时,结果将是这样的2018年9月17日晚上8:28:29 GMT+7
。然后我想将它存储在Firestore
中,稍后,当我查询它时,我可以根据当前日期和时间对它进行排序。例如,recent call it根据当前日期和时间在tableView上存储数据。我想实现这样的目标。我想用当前日期和时间将数据发布到Firestore。Firestore已经提供了数据类型为Timestamp
,其中包括日期和时间2018年9月17日格林尼治时间晚上8:28:29+7
。因此,在编程中,我想用Timestamp
数据类型将数据发布到Firestore。我想用当前日期和时间将数据发布到Firestore。Firestore已经提供了数据类型为Timestamp
,其中包括日期和时间2018年9月17日格林尼治时间晚上8:28:29+7
。因此,在编程中,我想用时间戳
数据类型将数据发布到Firestore。