gpt4 book ai didi

ios - ISO8601 格式在 MongoDB 和 iOS 中不同

转载 作者:太空宇宙 更新时间:2023-11-03 23:15:13 25 4
gpt4 key购买 nike

我想将 IOS8601 格式的 Date 从 iOS 发布到 NodeJS(mongoDB),并从 NodeJS 获取到 iOS。

但是两个平台的日期格式都不同。

iOS端:2019-05-26T17:10:10ZmongoDB端:2019-05-26T17:10:10.000Z

这意味着日期示例发布到 mongoDB,然后日期返回时带有额外的 .000

对于 iOS:

extension Date {
var ISO8601String: String? {
let formatter = ISO8601DateFormatter()
return formatter.string(from: self)
}
}

ISO8601String 输出为:YYYY-MM-ddTHH:mm:ssZ

对于 mongoDB

架构:

var FooSchema = Schema({

date: {
type: Date,
required: true
}
})

用法:

let date = new Date(req.body.date) // I also used req.body.date directlly
new Foo(date: date)

在 iOS 端获取添加了 .000 的 ISO8601 日期。这是yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

这是doc reference : enter image description here

我必须至少从一侧进行转换吗?

最佳答案

在 iOS 中,您可以在 iOS 11 及更高版本中生成包含毫秒的格式化程序:

let formatter = ISO8601DateFormatter()
formatter.formatOptions.insert(.withFractionalSeconds)

如果您需要在此之前的 iOS 版本上执行此操作,则必须使用 DateFormatter:

let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSX"
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)

关于ios - ISO8601 格式在 MongoDB 和 iOS 中不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56315853/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com