NSDateComponents 给出了错误的 2 月 28 日日期
我正尝试在二月内进行搜索。所以我想弄清楚最后一天是 28 还是 29。我正在从 3 月底做减法,
NSDate* endFeb = [march dateByAddingTimeInterval: (NSTimeInterval) -24 * 3600];
NSDateComponents *feb = [gregorian components: units fromDate: endFeb];
这给了我正确的日期,但不是正确的日期。
(gdb) po [gregorian dateFromComponents: feb]
2011-02-28 00:00:00 +0000
(gdb) p (int) [feb day]
$1 = 27
如果重要的话,这是在 iPad 4.3 模拟器上。
I'm attempting to do a search within February. So I want to figure out if the last day is 28 or 29. I'm doing a subtraction from the end of March
NSDate* endFeb = [march dateByAddingTimeInterval: (NSTimeInterval) -24 * 3600];
NSDateComponents *feb = [gregorian components: units fromDate: endFeb];
which gives me the correct date but not the right day
(gdb) po [gregorian dateFromComponents: feb]
2011-02-28 00:00:00 +0000
(gdb) p (int) [feb day]
$1 = 27
This is on the iPad 4.3 simulator if it matters.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要确定 2 月是 28 天还是 29 天,您可以使用类似以下的代码:
这样您就不必弄乱时区偏移。我猜这是你的代码的问题。
To figure out if february has 28 or 29 days you would use something like this:
This way you don't have to mess with timezone offsets. I guess this is the problem with your code.