Swift 5中从日期时间字符串到当前时间的天时差

问题描述 投票:0回答:1

我有一个DateTime字符串为“ IST 15/06/2019 02:03:20 PM”。我需要计算DateTime字符串与当前日期和时间之间的天时差。

我可以使用]获得currentDateTime>

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd/MM/yyyy hh:mm:ss a zzz"
let now = Date()
print(dateFormatter.string(from: now))      //It gives output as 06/10/2019 11:08:52 AM GMT+5:30 not as IST timezone. 

但是我无法将给定的DateTime字符串转换为日期对象以获取差异。

let registrationTime = "15/06/2019 02:03:20 PM IST"
print(dateFormatter.date(from: registrationTime))       //prints nil

我尝试将日历,语言环境和时区添加到dateformatter,但未返回date对象。我还尝试了将组件中的registationTime打破(由whiteSpace分隔),并将它们分别从字符串转换为日期,但这还是行不通的。

我有一个DateTime字符串为“ IST 15/06/2019 02:03:20 PM”。我需要计算DateTime字符串与当前日期和时间之间的天时差。我能够获得currentDateTime ...

swift datetime date-difference dateformatter
1个回答
0
投票

要使其与印度标准时间配合使用,您需要将印度的语言环境设置为一个语言环境

© www.soinside.com 2019 - 2024. All rights reserved.