NSDate、NSCalendar 和 NSDateComponents 计时
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+2"]];
NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];
NSLog(@" %i %i %i ", day, month, year);
该代码显示“12 2147483647 2147483647”
如何获取月份和年份(整数)
如何添加/转发一天? (如果我们也是这个月的第一天!)
感谢您的关注:-)
NSDate *today = [NSDate date];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+2"]];
NSDateComponents *dateComponents = [calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [dateComponents day];
NSInteger month = [dateComponents month];
NSInteger year = [dateComponents year];
NSLog(@" %i %i %i ", day, month, year);
That code show me "12 2147483647 2147483647"
How can I get the month and year (integer)
How can add/forward one day? (if we are the first of the month too!)
Thant you for your attention :-)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
希望这有帮助
hope this helps
向 NSDayCalendarUnit | 添加更多单位NSWeekdayCalendarUnit - 您没有请求月份或年份,因此它们无效!
添加/减去配置为 1 天的
NSDateComponents
对象和[NSCalendar dateByAddingComponents:toDate:options:]
。或者,也可能同样正确,使用[NSDate dateWithTimeInterval:sinceDate:]
Add more units to
NSDayCalendarUnit | NSWeekdayCalendarUnit
- you are not requesting month or year, therefore they are invalid!Add/subtract a
NSDateComponents
object configured for 1 day and[NSCalendar dateByAddingComponents:toDate:options:]
. Alternatively, and probably just as correct, use[NSDate dateWithTimeInterval:sinceDate:]