NSDate 来自 NSString ,显示前一天 [英] NSDate from NSString , shows the previous day

查看:51
本文介绍了NSDate 来自 NSString ,显示前一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有来自 NSString 的简单 NSDate 如下

I have s imple NSDate from NSString as below

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                    issueDatabase.album_date = [dateFormatter dateFromString:albumXML.album_date];

但由于某种原因,如果我的

but for some reason I'm getting the previous day instead of the current for eample if my

albumXML.album_date 等于 2011-09-01 我将以下内容存储在我的数据库中 2011-08-31

albumXML.album_date is equal 2011-09-01 I'm getting the following stored in my database 2011-08-31

推荐答案

您需要为 dateFormatter 设置时区:

You need to set a timezone to your dateFormatter:

[dateFormatter setTimeZone:[NSTimeZone defaultTimeZone]];

如果不这样做,您可能会获得 GMT 时区,这可能会出现在过去,具体取决于您在地球上的位置...

If you don't, you're probably getting the GMT timezone, which could appear in the past depending on where you are on the planet...

这篇关于NSDate 来自 NSString ,显示前一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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