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

标签 ios swift firebase google-cloud-firestore

我想了解日期是如何准确地保存在 Firebase 的 CloudFirestore 中的...

当我将当前日期放入数据库 dateToSAve = Date() 时,此日期使用 UTC-7 存储:

enter image description here

但是以法国人为例,想在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/

相关文章:

ios - 将字典数组传递给函数的问题

ios - 检索图像到我的 UIImageView

javascript promise 不会嵌套并返回使用 firebase 云函数的通知错误

firebase - 如何从与 Firebase 同步的 AngularFire 集合中获取单个记录

ios - 表格 View 中动态单元格高度中的标签未正确显示文本

ios - Swift 变量声明是如何工作的

ios - XCode iOS 用 + 替换空格

objective-c - 在 iOS 中设置标签文本时遇到问题

ios - Swift:协议(protocol) vs. 结构 vs. 类

ios - 无法使用 iOS Swift API 将图片上传到 Google Firebase 存储