ios - 从当前时区转换为 UTC 是不正确的 swift
问题描述
我有一个函数可以将时间从我当前的 TimeZone 转换为 UTC。
static func sendFormattedTimeString(_ string: String?) -> String {
let dateFormatterGet = DateFormatter()
dateFormatterGet.dateFormat = "HH:mm"
dateFormatterGet.timeZone = TimeZone.current
let dateFormatterPrint = DateFormatter()
dateFormatterPrint.dateFormat = "HH:mm:ss"
dateFormatterPrint.timeZone = TimeZone(identifier: "UTC")
if let string = string,
let date = dateFormatterGet.date(from: string) {
return dateFormatterPrint.string(from: date)
}
else {
print("There was an error decoding the string")
return ""
}
}
我的位置是白俄罗斯。现在是格林威治标准时间 +3。但在 UTC 中返回 +1。示例:我的当前时间:16:00。UTC 返回:14:00。
有什么问题?感谢您的回答。
解决方案
尝试使用“TimeZone.current.secondsFromGMT()”,而不是执行上述所有操作,它会返回秒数,您的工作会将其转换为小时和分钟。
推荐阅读
- arrays - 编写 MIPS (32) 汇编代码将 A[2] 的内容与 B[2] 交换
- python - 如何使用列表中的正则表达式从 web 元素中提取字符串(python)
- unity3d - 无法在场景视图或游戏视图中查看 TextMeshPro (TMP) 文本
- java - 如何使用 JAVA 在 Selenium 中集成 API 和 UI 框架?
- excel - 用另一个选项卡上的表中的值替换一个选项卡上的现有值
- html - Visual Studio 代码调试栏
- python - 如何在 Pyspark 中并行化 for 循环?
- python - 根据多个条件删除 pyspark 上的行的最佳方法是什么?
- python - 循环遍历 Pandas 系列以生成字典
- javascript - DOM 元素数据集包含带有 (this) 参数的调用