ios - Ios Swift 日期从 TimeZone 转换
问题描述
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = TimeZone(abbreviation: "KST")
let nowstring = dateFormatter.string(from: Date())
let nowdate = dateFormatter.date(from: nowstring)
print(nowstring)
print(nowdate)
打印这个2018-07-24T15:06:26
Optional(2018-07-24 06:06:26 +0000)
我想要现在日期值2018-07-24T15:06:26
但是没有时区的字符串到日期
我必须使用,timeIntervalSince
所以 nowdate 必须有时区
解决方案
let today = NSDate()
print("\(today)")
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
let nowDatestring = formatter.string(from: today)
formatter.timeZone = TimeZone(abbreviation: "KST")
let kstDate = formatter.date(from: nowDatestring)
print("\(kstDate!)")
formatter.timeZone = TimeZone(abbreviation: "PST")
let pstDate = formatter.date(from: nowDatestring)
print("\(pstDate!)")
formatter.timeZone = TimeZone(abbreviation: "IST")
let istDate = formatter.date(from: nowDatestring)
print("\(istDate!)")
推荐阅读
- java - 为什么这个函数打印它的作用?
- javascript - 如何在谷歌地图上添加带有不同图标的标记?
- wordpress - 如何将“-”符号更改为管道符号“|” 在 WordPress 标题中?
- scala - 无法坚持演员
- excel - 等待 Windows 是否仍在使用该文件
- ehcache - 如何将 ehcache.xml 中的属性外部化
- ruby-on-rails - Rails 分页表单偶尔不会发布参数
- c++ - 将代码从 vb.net 迁移到 C++ dll 是否会显着提升性能?
- node.js - 导入模块后未定义类型错误读取文件
- jquery - 如何将 PHP 转换为 jQuery