objective-c 如何使用可可查找任何给定日期的星期几
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1057349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find what day of the week for any given date using Cocoa
提问by ThibThib
I'm trying to figure out what day (i.e. Monday, Friday...) of any given date (i.e. Jun 27th, 2009)
我试图找出任何给定日期(即 2009 年 6 月 27 日)的哪一天(即星期一、星期五...)
Thank you.
谢谢你。
回答by Jeff Hellman
I've been doing:
我一直在做:
NSDateFormatter* theDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[theDateFormatter setFormatterBehavior:NSDateFormatterBehavior10_4];
[theDateFormatter setDateFormat:@"EEEE"];
NSString *weekDay = [theDateFormatter stringFromDate:[NSDate date]];
This has the added bonus of letting you choose how you'd like the weekday to be returned (by changing the date format string in the setDateFormat: call
这有一个额外的好处,让您选择您希望返回工作日的方式(通过更改 setDateFormat 中的日期格式字符串:调用
Lots more information at:
更多信息请访问:
回答by ThibThib
You may have a look to the NSDate and NSCalendar classes. For example, hereand here
您可以查看 NSDate 和 NSCalendar 类。例如,这里和这里
They provide the following code:
他们提供以下代码:
NSDate *today = [NSDate date];
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *weekdayComponents =
[gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:today];
NSInteger day = [weekdayComponents day];
NSInteger weekday = [weekdayComponents weekday];
回答by Chuck
Use NSCalendar and NSDateComponents. As shown in the NSDateComponents documentation:
使用 NSCalendar 和 NSDateComponents。如 NSDateComponents 文档中所示:
NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];
回答by SpokaneDude
Here's what I wound up with, which works exactly as intended. It takes a date, alters the date to be the first of the month, and gets the dayof that date:
这就是我的结论,它完全按预期工作。它需要一个日期,将日期更改为该月的第一天,并获取该日期的第几天:
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
NSDateComponents *monthDateComponents = [[NSCalendar currentCalendar] components: NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear |
NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond fromDate: date];
// build date as start of month
monthDateComponents.year = components.year;
monthDateComponents.month = components.month;
monthDateComponents.day = 1;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
// [gregorian setFirstWeekday:1];
NSDate *builtDate =[gregorian dateFromComponents: monthDateComponents];
// NSDateComponents *weekdayComponents =[gregorian components: NSCalendarUnitWeekday fromDate: builtDate];
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"E"];
NSString *firstDay = [df stringFromDate:builtDate];
if([firstDay isEqual:@"Sun"]) // do for the other 6 days as appropriate
return 7;

