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计时所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)