Firebase&Swift - 使用UTC -7保存的日期?

Kev*_*inB 1 ios firebase swift google-cloud-firestore

我试图了解在Firebase的CloudFirestore中保存日期的具体日期...

当我将当前日期放在数据库中时dateToSAve = Date(),此日期与UTC-7一起存储:

在此输入图像描述

但是在法国的某个人,想要在Firebase上保存数据,它将是当前日期-9小时符合UTC -7

有时,因此,它可能是另一天......你知道如何处理这个问题吗?我想在数据库中保存用户的当前日期.

Mee*_*riq 8

您不应将日期对象存储在数据库中,而应将时间戳保存为UTC 0.并且在检索该时间戳后,您可以将其转换回日期,并将当前时区中的时间部分转换回日期.

要获得UTC 0中的时间戳:

let timestamp = Date().timeIntervalSince1970
Run Code Online (Sandbox Code Playgroud)

并使用当前时区中的时间部分将时间戳转换回日期:

// gives date with time portion in UTC 0
let date = Date(timeIntervalSince1970: timestamp)

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM-dd-yyyy"  // change to your required format
dateFormatter.timeZone = TimeZone.current

// date with time portion in your specified timezone
print(dateFormatter.string(from: date)) 
Run Code Online (Sandbox Code Playgroud)