gpt4 book ai didi

iphone - 字符串 2012-11-09T00 :00:00 to short Form of date in iphone

转载 作者:行者123 更新时间:2023-12-03 19:43:52 24 4
gpt4 key购买 nike

我有一个字符串格式的日期

2012-11-09T00:00:00

我需要将其设置为 day=Fri Date-09

我尝试了很多方法,但日期打印为空。有人可以帮我吗?我想在我创建的自定义日历中显示它。

最佳答案

使用以下代码获取所需的值

NSString *dateStr = @"2012-11-09T00:00:00";
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"GMT"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss"];
NSDate *date = [dateFormatter dateFromString: dateStr];

NSLog(@"Date : %@", date);

NSCalendar *calendar = [NSCalendar currentCalendar];

NSDateComponents *dateComponents =
[calendar components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];

NSInteger weekday = [dateComponents weekday];
NSInteger monthdate = [dateComponents day];

NSLog(@"Weekday : %d Month date %d ", weekday, monthdate);

我们获取的工作日为 1 到 7 之间的整数。您必须相应地转换工作日。

关于iphone - 字符串 2012-11-09T00 :00:00 to short Form of date in iphone,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13119208/

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