要获取当前的年、月、日,可以使用NSDateNSCalendar类来实现。下面是通过Objective-C获取当前年月日的示例代码:

// 获取当前日期
NSDate *currentDate = [NSDate date];

// 创建一个用来处理日期的日历对象
NSCalendar *calendar = [NSCalendar currentCalendar];

// 获取当前日期的年份
NSDateComponents *yearComponent = [calendar components:NSCalendarUnitYear fromDate:currentDate];
NSInteger year = [yearComponent year];

// 获取当前日期的月份
NSDateComponents *monthComponent = [calendar components:NSCalendarUnitMonth fromDate:currentDate];
NSInteger month = [monthComponent month];

// 获取当前日期的天数
NSDateComponents *dayComponent = [calendar components:NSCalendarUnitDay fromDate:currentDate];
NSInteger day = [dayComponent day];

// 输出结果
NSLog(@"Year: %ld", (long)year);
NSLog(@"Month: %ld", (long)month);
NSLog(@"Day: %ld", (long)day);

以上代码将会输出当前日期的年、月、日。请注意,这里的年、月、日都是以整数形式表示的

ios 获取当前年月日 object-c

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

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