如何从Firebase firestore获取正确的iOS时间戳?
我有一个名为playerLoginTime: November 30, 2018 at 4:12:20 PM UTC+5:30
的字段
但是当我尝试获取结果时,却得到了错误的结果。
2018-11-30 10:47:12 +0000
我想以MMM dd,yyyy
的形式获取日期。
以下我的查询
let docRef = db.collection("employees").document(userID)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
if document.get("playerLoginTime") != nil {
self.loginTime = document.get("playerLoginTime") as! Date;
print("Before printdates \(self.loginTime )")
}
}
答案 0 :(得分:0)
这是您的解决方案:
let docRef = db.collection("employees").document(userID)
docRef.getDocument { (document, error) in
if let document = document, document.exists {
if document.get("playerLoginTime") != nil {
self.loginTime = document.get("playerLoginTime") as! Date;
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MM-dd-yyyy"
self.loginTime = dateFormatter.string(from: self.loginTime)
}
}
答案 1 :(得分:0)
我想我也有这个问题。
我通过将其转换为Firebase类型的package com;
import java.util.*;
import java.io.*;
/*to print unique words which are not repeated*/
public class UniqueWords {
public static final void main(String[] args){
ArrayList<String> allWords = new ArrayList<String>();
Map<String, Integer> map = new HashMap<String, Integer>();
String fileName = "C:\\TestFiles\\MultiLineStory.txt";
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(fileName));
String input = null;
if((input = reader.readLine()) != null){
String arr[] = input.split(" ");
allWords.addAll(Arrays.asList(arr));
}
System.out.println(allWords.size());
}catch(FileNotFoundException fnfe){
System.out.println("Unable to find file: " + fileName);
} catch(IOException ioe){
ioe.printStackTrace(System.err);
} finally {
try{
reader.close();
}catch(Exception e){}
}
for(String word : allWords)
{
if(!map.containsKey(word))
map.put(word, 1);
else
map.put(word, map.get(word)+1);
}
Set<String> keySet = map.keySet();
for(String key : keySet)
if(map.get(key)==1)
System.out.println(key);
}
}
而不是TimeStamp
来修复它,然后在显示之前用Date
转换为Date
。
要获得dateFormatter
类型,只需将Firebase导入到需要它的文件中即可。
奖金提示:我建议您使用SwiftDate库,而不是自己进行转换(并且如果您不介意开放源代码库)!它非常易于使用,并且从TimeStamp
到TimeStamp
到格式化日期的转换非常容易。此外,您还可以轻松地转换为类似Twitter的时间戳(即2分钟前为Date
)
希望有帮助!