ios - 如何在 Swift 中将本地时间的日期转换为 UTC?
<p><p>假设我在本地时间有一个字符串:“Apr 14, 2015, 8:53 AM”。如何将其转换为正确的 UTC 时间?</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>试试这样:</p>
<pre><code>let dateString = "Apr 14, 2015, 8:53 AM"
let df = NSDateFormatter()
df.dateFormat = "LLL DD, yyyy, h:mm a"
if let dateFromString = df.dateFromString(dateString){
print(dateFromString)// "2015-01-14 10:53:00 +0000\n"
df.timeZone = NSTimeZone(forSecondsFromGMT: 0)
let stringFromDate = df.stringFromDate(dateFromString) //"Jan 14, 2015, 10:53 AM"
}
</code></pre></p>
<p style="font-size: 20px;">关于ios - 如何在 Swift 中将本地时间的日期转换为 UTC?,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36631232/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36631232/
</a>
</p>
页:
[1]