dateformatter显示不正确的时间
我的要求是将从我的API收到的日期字符串(以PHT为单位)转换为另一种日期字符串格式。
- API日期字符串格式:“ MM/DD/YYYY HH:MMA” EG。 “ 06/28/2022 06:55 PM”
- UI日期字符串格式:“ DD MMM Yyyy,HH:MM A”,例如。 “ 2022年6月28日,下午06:55”
我写了以下代码:
extension Date {
func toString(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
init?(from: String) {
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy HH:mma"
guard let date = formatter.date(from: from) else { return nil }
self = date
}
}
let dateFormat = "dd MMM yyyy, hh:mm a"
var dateStr = "06/28/2022 06:55PM"
let date = Date(from: dateStr)
date?.toString(format: dateFormat)
但是,时间不正确。我将“ 2022年6月28日,下午12:55”作为输出。我也尝试添加语言环境和时区。似乎没有任何帮助。任何帮助都将不胜感激。
编辑:对于编译问题,我深表歉意。最后,我编辑了几个变量名称,以使其更可读性,错误地删除了几个方法。我已经修复并更新了直接粘贴在操场文件中时运行的代码。
@Larme:谢谢您的回答。它奏效了!您能否将其作为一个单独的答案发布,以便我可以接受。
My requirement is to convert date string (in PHT) received from my API to another date string format.
- API Date String format: "MM/dd/yyyy HH:mma" eg. "06/28/2022 06:55PM"
- UI Date String Format: "dd MMM yyyy, hh:mm a", eg. "28 Jun 2022, 06:55 PM"
I wrote following code:
extension Date {
func toString(format: String) -> String {
let formatter = DateFormatter()
formatter.dateFormat = format
return formatter.string(from: self)
}
init?(from: String) {
let formatter = DateFormatter()
formatter.dateFormat = "MM/dd/yyyy HH:mma"
guard let date = formatter.date(from: from) else { return nil }
self = date
}
}
let dateFormat = "dd MMM yyyy, hh:mm a"
var dateStr = "06/28/2022 06:55PM"
let date = Date(from: dateStr)
date?.toString(format: dateFormat)
However, the time is incorrect. I get "28 Jun 2022, 12:55 PM" as the output. I tried adding locale and timeZone as well. Nothing seems to help. Any help would be much appreciated.
EDIT: I apologise for compilation issues. I edited few variable names at the end to make it more readable, erroneously deleted few method calls. I have fixed and updated the code to run when directly pasted in Playground file.
@Larme : Thank you for your answer. It worked! Could you please post it as a separate answer so that I can accept it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
@Larne呼吁从“ mm/dd/yyyy hh:mma”更改为“ mm/dd/yyyy hh:mma”,并且即兴犯了以下错误代码
As @Larne's call to change from "MM/dd/yyyy HH:mma" to "MM/dd/yyyy hh:mma" and improvised of mistakes code like below
就我个人而言,我处理与此库中Swift中的日期相关的所有内容 swiftdate 。太好了。我为您留下一个例子,说明这种事情有多简单。
输出是:
要理解的主要内容是,您要解析的字符串以ISO格式不包含任何区域信息,因此,如果您尝试使用UTC/GMT等进行解析,我不认为您将获得正确的价值。例如,如果您使用UTC(不包括AM/PM)进行解析,则它可以使用,但是如果您在输入格式中包含“ A”,并且在日期字符串中添加AM或PM,则将无法解析它。
Personally, I handle everything related to dates in Swift with this library SwiftDate. It's just great. I leave you an example of how simple this kind of thing is.
The output is:
The main thing to understand is that the string you are trying to parse is in ISO format since it doesn't contain any zone information and therefore if you tried to parse it with UTC/GMT etc, I don't think you would get the correct value. For example if you parse using UTC without including AM/PM it works, but if you include 'a' in the input format and AM or PM in the date string it won't be able to parse it.
首先:您的时间转换问题是通过使用 hh 而不是输入格式中的 hh 引起的。
然后,我建议不要像扩展中那样在函数调用中实例化dateformatter,因为这是一个相对昂贵的操作。
创建一次时间格式器并重复使用:
在此通用名称下使用特定功能延长日期也可能会在应用程序中其他地方迅速导致搞笑行为,如果不正确范围。
还要记住,如果您不在输入上设置时区,则假设字符串与API服务同一时区显示。因此,如果您的用户不在PHT中,则时间显示将关闭(尽管用户在其他地方,但它将显示PHT时间)。
希望它有帮助!
First: Your time conversion issue was caused by using HH instead of hh in your input format.
Then I would advice to never instantiate a DateFormatter within a function call as you did in your extension, as this is a relatively expensive operation.
Create the time formatters once and reuse them:
Extending Date with specific functionality under such a generic name can also quickly lead to funny behavior elsewhere in your app if not properly scoped.
Also keep in mind that if you don't set the timezone on the input, then you are assuming that the string is displayed in the same time zone as the API service. So if your user is not in PHT, the time display will be off (it will display PHT time, though the user is somewhere else).
Hope it helps!