iphone – NSDate,NSCalendar和NSDateComponents计时

iphone – NSDate,NSCalendar和NSDateComponents计时,第1张

概述NSDate *today = [NSDate date];NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];[calendar setTimeZone:[NSTimeZone timeZoneWithName:@"GMT+2"]];NSDateCompone
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”

我怎样才能获得月份和年份(整数)

如何添加/转发一天? (如果我们也是这个月的第一个!)

请你注意:-)

解决方法
NSCalendar *calendar= [[NSCalendar alloc] initWithCalendarIDentifIEr:NSGregorianCalendar];NSCalendarUnit unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit | NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit;NSDate *date = [NSDate date];NSDateComponents *dateComponents = [calendar components:unitFlags fromDate:date];NSInteger year = [dateComponents year];NSInteger month = [dateComponents month];NSInteger day = [dateComponents day];NSInteger hour = [dateComponents hour];NSInteger minute = [dateComponents minute];NSInteger second = [dateComponents second];[calendar release];

希望这可以帮助

总结

以上是内存溢出为你收集整理的iphone – NSDate,NSCalendar和NSDateComponents计时全部内容,希望文章能够帮你解决iphone – NSDate,NSCalendar和NSDateComponents计时所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存