"获取当前日期:年、月、日" "使用 Objective-C 获取当前的年、月、日,可以使用NSDateNSCalendar类来实现。下面是通过Objective-C获取当前年月日的示例代码:\n\nobjective-c\n// 获取当前日期\nNSDate *currentDate = [NSDate date];\n\n// 创建一个用来处理日期的日历对象\nNSCalendar *calendar = [NSCalendar currentCalendar];\n\n// 获取当前日期的年份\nNSDateComponents *yearComponent = [calendar components:NSCalendarUnitYear fromDate:currentDate];\nNSInteger year = [yearComponent year];\n\n// 获取当前日期的月份\nNSDateComponents *monthComponent = [calendar components:NSCalendarUnitMonth fromDate:currentDate];\nNSInteger month = [monthComponent month];\n\n// 获取当前日期的天数\nNSDateComponents *dayComponent = [calendar components:NSCalendarUnitDay fromDate:currentDate];\nNSInteger day = [dayComponent day];\n\n// 输出结果\nNSLog(@"Year: %ld", (long)year);\nNSLog(@"Month: %ld", (long)month);\nNSLog(@"Day: %ld", (long)day);\n\n\n以上代码将会输出当前日期的年、月、日。请注意,这里的年、月、日都是以整数形式表示的。

Objective-C 获取当前日期:年、月、日

原文地址: https://www.cveoy.top/t/topic/qE7G 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录