首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何使用Foundation获取一周中的某天?

如何使用Foundation获取一周中的某天?
EN

Stack Overflow用户
提问于 2010-11-25 00:30:43
回答 10查看 82.4K关注 0票数 110

如何将星期几作为字符串获取?

EN

回答 10

Stack Overflow用户

回答已采纳

发布于 2010-11-25 00:40:21

NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];  
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"%@", [dateFormatter stringFromDate:[NSDate date]]);

根据当前区域设置,将当前星期几输出为区域设置中的字符串。

要只获取星期几的数字,必须使用NSCalendar类:

NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
NSDateComponents *comps = [gregorian components:NSWeekdayCalendarUnit fromDate:[NSDate date]];
int weekday = [comps weekday];
票数 216
EN

Stack Overflow用户

发布于 2012-09-14 06:45:01

只需使用以下三行:

CFAbsoluteTime at = CFAbsoluteTimeGetCurrent();
CFTimeZoneRef tz = CFTimeZoneCopySystem();
SInt32 WeekdayNumber = CFAbsoluteTimeGetDayOfWeek(at, tz);
票数 18
EN

Stack Overflow用户

发布于 2015-07-28 23:14:34

这里的许多答案都被弃用了。从iOS 8.4开始,它以字符串和数字的形式为您提供星期几。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"EEEE"];
NSLog(@"The day of the week: %@", [dateFormatter stringFromDate:[NSDate date]]);

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSDateComponents *comps = [gregorian components:NSCalendarUnitWeekday fromDate:[NSDate date]];
int weekday = [comps weekday];
NSLog(@"The week day number: %d", weekday);
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4269093

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档