gpt4 book ai didi

iphone - NSDate、NSCalendar 和 NSDateComponents 计时

转载 作者:行者123 更新时间:2023-12-03 16:12:32 25 4
gpt4 key购买 nike

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 计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5634067/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com