NSDate问题,日期应该是前一天

发布于 2024-12-05 18:24:00 字数 830 浏览 0 评论 0原文

我想实现方法,该方法将计算一个月的天数。一切都很好,除了我的约会是前一天。在以下示例中,日期为“2012-09-14 21:00:00 +0000”,应为 2012-09-15 21:00:00 +0000 将感谢您的帮助。我的代码是:

NSDictionary *curDay = [NSDictionary dictionaryWithObjectsAndKeys:
@"9",@"mounthNumber",@"15",@"day",@"2012",@"year", nil];
NSDateComponents *selectedMounth = [[[NSDateComponents alloc] init] autorelease];
NSCalendar *calendar = [[[NSCalendar alloc]initWithCalendarIdentifier:
NSGregorianCalendar] autorelease];
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
[calendar setTimeZone: timezone];
[selectedMounth setYear:[[curDay objectForKey:@"year"] integerValue]];
[selectedMounth setMonth:[[curDay objectForKey:@"mounthNumber"] integerValue]];
[selectedMounth setDay:[[curDay objectForKey:@"day"] integerValue]];
NSDate *date = [calendar dateFromComponents:selectedMounth];

I want to implement method, that will count the number of days in month. Everything is fine except my date is the day before then it should be. In the following example date is "2012-09-14 21:00:00 +0000" and should be 2012-09-15 21:00:00 +0000 Will appreciate for your help. My code is:

NSDictionary *curDay = [NSDictionary dictionaryWithObjectsAndKeys:
@"9",@"mounthNumber",@"15",@"day",@"2012",@"year", nil];
NSDateComponents *selectedMounth = [[[NSDateComponents alloc] init] autorelease];
NSCalendar *calendar = [[[NSCalendar alloc]initWithCalendarIdentifier:
NSGregorianCalendar] autorelease];
NSTimeZone *timezone = [NSTimeZone systemTimeZone];
[calendar setTimeZone: timezone];
[selectedMounth setYear:[[curDay objectForKey:@"year"] integerValue]];
[selectedMounth setMonth:[[curDay objectForKey:@"mounthNumber"] integerValue]];
[selectedMounth setDay:[[curDay objectForKey:@"day"] integerValue]];
NSDate *date = [calendar dateFromComponents:selectedMounth];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单调的奢华 2024-12-12 18:24:00

你的时区似乎是 GMT-3(或 GMT+3,我不记得了)。使用 GMT 代替 systemTimeZone[NSTimeZone timeZoneWithAbbreviation:@"GMT"]; 它可以解决您的问题。

Your timeZone seems to be something like GMT-3 (or GMT+3, I never remember). Instead of the systemTimeZone use the GMT one:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]; and it solves your problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文