我想了解日期是如何准确地保存在 Firebase 的 CloudFirestore 中的...
当我将当前日期放入数据库 dateToSAve = Date()
时,此日期使用 UTC-7 存储:
但是以法国人为例,想在Firebase上保存数据,会是当前日期-9小时,以符合UTC-7
有时,正因为如此,它可能是不同的一天...... 你知道如何处理这个问题吗?我想在数据库中保存用户的当前日期。
最佳答案
您不应将日期对象存储在数据库中,而应将时间戳保存为 UTC 0。检索该时间戳后,您可以将其转换回具有当前时区时间部分的日期。
获取 UTC 0 时间戳:
let timestamp = Date().timeIntervalSince1970
并将时间戳转换回当前时区的时间部分:
// 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))
关于ios - Firebase & Swift - 使用 UTC -7 保存的日期?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52030883/