我想在2018年4月20日晚上9:02:00获得这种类型的甲酸盐->,UTC+5:30作为存储时间戳。我已经尝试了这段代码,但没有像上面那样得到正确的格式。
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM d yyyy hh:mm:ss a Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
let result = formatter.string(from: date)
print(result)已使用此格式化程序formatter.dateFormat = "MMMM‘this :mm:ss Z“
谢谢
发布于 2018-04-23 09:25:43
你可以这样使用:
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z" //If you dont want static "UTC" you can go for ZZZZ instead of 'UTC'Z.
formatter.timeZone = TimeZone(abbreviation: "IST")
let result1 = formatter.string(from: date)
print(result1)备注:如果不希望UTC在字符串中是静态的,也可以使用格式
"MMMM dd, yyyy 'at' hh:mm:ss a ZZZZ"
感谢 Larme 的格式改进
输出:

发布于 2018-04-23 09:27:33
喜欢
let formatter = DateFormatter()
//If you want it in your specific format you can simply add 'UTC'
formatter.dateFormat = "MMMM dd, yyyy 'at' hh:mm:ss a 'UTC'Z"
formatter.timeZone = NSTimeZone(name: "IST") as TimeZone?
let result = formatter.string(from: Date())
print(result)注意:如果您想为您的字符串设置currentTimezone,那么使用格式"MMMM,yyyy 'at‘hh:mm:ss ZZZZ"
您得到的输出为

发布于 2018-04-23 09:41:23
如果你不想在小时内,当小时有个位数时,你就试一试。此外,您也不需要将NSTimeZone键入到TimeZone中。您可以直接使用TimeZone。
let date = Date()
let formatter = DateFormatter()
formatter.dateFormat = "MMMM dd, yyyy 'at' h:mm:ss a 'UTC'Z"
formatter.timeZone = TimeZone(abbreviation: "IST")
let result = formatter.string(from: date)
print(result)2018年4月23日下午3:09:01 UTC+0530

https://stackoverflow.com/questions/49977414
复制相似问题