objective-c – 修改NSDate从今天起1个月

objective-c – 修改NSDate从今天起1个月,第1张

概述我将重复的事件添加到我正在开发的Cocoa应用程序中。我每天和每周都要重复,因为我可以数学定义(3600 * 24 * 7 = 1周)。我使用以下代码修改日期: [NSDate dateWithTimeIntervalSinceNow:(3600*24*7*(weeks))] 我知道自事件重复以来已经过去了几个月,但是我未能确定如何制作一个NSDate对象,这个对象将来代表1个月/ 3个月/ 6个 我将重复的事件添加到我正在开发的Cocoa应用程序中。我每天和每周都要重复,因为我可以数学定义(3600 * 24 * 7 = 1周)。我使用以下代码修改日期:

[NSDate dateWithTimeIntervalSinceNow:(3600*24*7*(weeks))]

我知道自事件重复以来已经过去了几个月,但是我未能确定如何制作一个NSDate对象,这个对象将来代表1个月/ 3个月/ 6个月/ 9个月。理想情况下,我希望用户从10月14日起每月重复一次,每月14号重复一次。

解决方法 (几乎和 this question相同)

从documentation:

Use of NSCalendarDate strongly
discouraged. It is not deprecated yet,
however it may be in the next major OS
release after Mac OS X v10.5. For
calendrical calculations,you should
use suitable combinations of
NSCalendar,NSDate,and
NSDateComponents,as described in
Calendars in 07002.

接下来的建议:

NSDate *today = [NSDate date];NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIDentifIEr:NSGregorianCalendar];NSDateComponents *components = [[NSDateComponents alloc] init];components.month = 1;NSDate *nextMonth = [gregorian dateByAddingComponents:components toDate:today options:0];[components release];NSDateComponents *nextMonthComponents = [gregorian components:NSYearCalendarUnit | NSMonthCalendarUnit fromDate:nextMonth];NSDateComponents *todayDayComponents = [gregorian components:NSDayCalendarUnit fromDate:today];nextMonthComponents.day = todayDayComponents.day;NSDate *nextMonthDay = [gregorian dateFromComponents:nextMonthComponents];[gregorian release];

可能有更直接或更有效的实施,但这应该是准确的,应该指向正确的方向。

总结

以上是内存溢出为你收集整理的objective-c – 修改NSDate从今天起1个月全部内容,希望文章能够帮你解决objective-c – 修改NSDate从今天起1个月所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1035594.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存