[英]NSDate with no time
I have written the following method: 我写了以下方法:
- (NSDate *)stringToDate:(NSString *)dateString
{
// Convert string to date
NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init];
NSTimeZone *tz = [NSTimeZone timeZoneWithName:@"America/New_York"];
[dateFormat setDateFormat:@"M/d/yyyy HH:mm:ss"];
[dateFormat setTimeZone:tz];
NSDate * date = [dateFormat dateFromString:[NSString stringWithFormat:@"%@ 00:00:00", dateString]];
return dateOnly;
}
When I call this with just a date such as 11/1/2013 or 11/13/2013 I get 2013-11-01 04:00:00 +0000 and 2013-11-13 05:00:00 +0000. 当我仅用诸如11/1/2013或11/13/2013之类的日期进行调用时,我得到2013-11-01 04:00:00 +0000和2013-11-13 05:00:00 +0000。
If I set a breakpoint on the return the date appears right, but if I break at in the calling function after this call, the date is returned with the time. 如果我在返回值上设置断点,则日期显示正确,但是如果我在此调用后在调用函数处中断,则日期和时间一起返回。
How come my time is not always 0. Can anyway tell me what is wrong in my function? 为什么我的时间并不总是为0。无论如何,我可以告诉我我的功能出了什么问题吗?
Thank you 谢谢
UPDATE: 更新:
The input string is as follows: 11/1/2013 and 11/13/2013 输入字符串如下:11/1/2013和11/13/2013
NSDate is a point in time. NSDate是一个时间点。 It will always have a time component.
它将始终具有时间成分。
And if not printed as a string form a NSDateFormatter, the Date and time will always be the one of UTC/GMT. 而且,如果未以NSDateFormatter的形式打印为字符串,则日期和时间将始终为UTC / GMT之一。
The format and the date string must fit. 格式和日期字符串必须适合。
NSString *dateString = @"11/1/2013";
[dateFormat setDateFormat:@"M/d/yyyy"];
The one hour apart comes from the Daylight saving time. 相隔一小时来自夏令时。 Till November, 3rd 2013 New York has Summer time.
直到2013年11月3日,纽约才有夏令时。 http://www.timeanddate.com/worldclock/clockchange.html?n=179
http://www.timeanddate.com/worldclock/clockchange.html?n=179
Ok, so can I ignore that?
好吧,我可以忽略吗? I am trying to compare NSDates when I do my comparison fails because of the time part
由于时间原因我的比较失败时,我正在尝试比较NSDates
You should create dates with with a time during the day — ie noon — to be save of DST mess and compare those. 您应该使用一天中的某个时间(即中午)创建日期,以保存DST混乱并进行比较。 Use NSComponents for that.
为此使用NSComponents。
A must-see for any iOS/Cocoa-Developer: the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations" . 任何iOS / Cocoa开发人员都必须看的东西:梦幻般的WWDC 2011视频“ Session 117-Performing Calendar Calculations” 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.