NSDate 和 NSTimezone 的时间错误
例如,我有两个日期(用两个不同的 NSString 编写),
31/12/2011
我
31/03/2012
必须检查今天是否在这个范围内。
为了从 NSString 读取日期,我正在这样做:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *mydate = [dateFormatter dateFromString:my_string];
NSLog(@"%@", mydate);
但是,在这个例子中,日志是这个
2011-12-30 23:00:00 +0000
2012-03-30 23:00:00 +0000
所以它晚了一小时。 我知道使用
NSString *string = [dateFormatter stringFromDate:my_date];
我的字符串就可以了,但是我看不到今天是否在使用 NSString 的范围内(或者不是?)!
所以我必须使用NSDate,但是如何解决“时区”问题呢?
另外,我也有这个问题,使用
[NSDate date];
它一小时前返回我(我不想要它,因为如果有人在午夜检查,对于电话来说,这将是前一天!)
I have two dates (written in two different NSString), for example
31/12/2011
and
31/03/2012
I have to check if today is in this range.
For read the date from the NSString, I'm doing this:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy"];
[dateFormatter setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *mydate = [dateFormatter dateFromString:my_string];
NSLog(@"%@", mydate);
but, in this example, the log is this one
2011-12-30 23:00:00 +0000
2012-03-30 23:00:00 +0000
So it's one hour behind.
I know that using
NSString *string = [dateFormatter stringFromDate:my_date];
my string it will be ok, but I can't see if today is in the range using NSString (or not?)!
So I have to use NSDate, but how can I solve the "time zone" problem?
Also, I have this problem also using
[NSDate date];
It returns me one hour ago (and I don't want it, because if someone check at midnight, for the phone it will be the day before!)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容应该会有所帮助:
有关更多详细信息,请查看此问题 ...看起来很相似。 “EST”可以替换为所需的时区。
The following should help:
for more details, please check out This question ... It looks similar. The "EST" can be replaced with whichever time zone is required.
我可能是错的,但看起来在这种情况下您不需要关心时区。如果您只有 dd/MM/yyyy 格式的日期,当您使用 NSDateFormatter 解析它时,它将默认使用当前时区。因此,当您与今天的日期进行比较时,所有 3 个日期将位于同一时区,并且不会执行日期/时间调整。
I may be mistaken, but it looks, like you don't need to take care about time zones in this case. If you have just dates in format dd/MM/yyyy, when you will parse it with NSDateFormatter, it will use current timezone by default. So, when you will be comparing with your today's date all 3 dates will be in the same timezone and date/time adjusting will not be performed.