gpt4 book ai didi

iOS 8 - 获取当前日期为 DD/MM/YYYY

转载 作者:技术小花猫 更新时间:2023-10-29 10:55:40 25 4
gpt4 key购买 nike

任何人都可以给我一些如何获取日期的代码吗?

NSDateComponents *components = [[NSCalendarcurrentCalendar] component:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYearfromDate:[NSDatedate]]; 

NSString *day = [components day];
NSString *week = [components month];
NSString *year = [components year];

NSString *date = [NSString stringWithFormat:@"%@.%@.%@",day,week,year];

^^ 我的代码不工作:S

有没有一种方法可以让我在 1 周内等等获得明天的日期...

谢谢:)

最佳答案

您可以使用代码的变体,使用 NSCalendarcomponents 方法检索数字组件:

NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];

NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];

NSString *string = [NSString stringWithFormat:@"%ld.%ld.%ld", (long)day, (long)month, (long)year];

请注意,这是components,而不是component

或者,更好的是,您可以使用 NSDateFormatter:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"d.M.yyyy";
NSString *string = [formatter stringFromDate:[NSDate date]];

关于iOS 8 - 获取当前日期为 DD/MM/YYYY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27790657/

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