NSDateFormatter dateFromString 返回错误的日期 [英] NSDateFormatter dateFromString returning wrong date

查看:42
本文介绍了NSDateFormatter dateFromString 返回错误的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问.

我有一个字符串日期:示例 var dateString = "17-02-2015"

I have a string date: example var dateString = "17-02-2015"

我使用:

var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

var newDate = dateFormatter.dateFromString(dateString)

我得到输出:2015-01-16 23:02:00 +0000

为什么我错过了一天?是时区的原因吗?(我在 +1,而在日期时区是 0?).

Why am I missing a day? Is it because of the time zone? (I am at +1, and in the date the timezone is 0?).

我也尝试过设置时区:

dateFormatter.locale = NSLocale.currentLocale()
dateFormatter.timeZone = NSTimeZone.systemTimeZone()

但没有成功.

我错过了什么?

推荐答案

这是 UTC 时间,考虑到您的 LocalTime 是 1+,这是正确的

Thats the UTC time and it is correct considering the fact that your LocalTime is 1+

var dateString = "17-02-2015"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"

var newDate = dateFormatter.dateFromString(dateString)!

println(newDate.descriptionWithLocale(NSLocale.currentLocale())!)

这篇关于NSDateFormatter dateFromString 返回错误的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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