NSDate快速设置时区 [英] NSDate set timezone in swift

查看:57
本文介绍了NSDate快速设置时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从字符串返回预定义时区中的 NSDate

how can i return a NSDate in a predefined time zone from a string

let responseString = "2015-8-17 GMT+05:30"
var dFormatter = NSDateFormatter()
dFormatter.dateFormat = "yyyy-M-dd ZZZZ"
var serverTime = dFormatter.dateFromString(responseString)
println("NSDate : \(serverTime!)")

以上代码返回时间为

2015-08-16 18:30:00 +0000

推荐答案

如何在预定义的时区返回 NSDate?

how can i return a NSDate in a predefined time zone?

你不能.

NSDate 的实例不携带任何有关时区或日历的信息.它只是简单地标识了世界时的一个点.

An instance of NSDate does not carry any information about timezone or calendar. It just simply identifies one point in universal time.

你可以在任何你想要的日历中解释这个 NSDate 对象.Swift 的字符串插值(示例代码的最后一行)使用使用 UTC 的 NSDateFormatter(即输出中的+0000").

You can interpret this NSDate object in whatever calendar you want. Swift's string interpolation (the last line of your example code) uses an NSDateFormatter that uses UTC (that's the "+0000" in the output).

如果您希望 NSDate 的值作为当前用户日历中的字符串,您必须为此明确设置日期格式化程序.

If you want the NSDate's value as a string in the current user's calendar you have to explicitly set up a date formatter for that.

这篇关于NSDate快速设置时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆