简体   繁体   English

如何使用Cocoa查找任何给定日期的星期几

[英]How to find what day of the week for any given date using Cocoa

I'm trying to figure out what day (ie Monday, Friday...) of any given date (ie Jun 27th, 2009) 我想弄清楚任何特定日期(即2009年6月27日)的哪一天(即周一,周五......)

Thank you. 谢谢。

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中的日期格式字符串:call

Lots more information at: 有关更多信息,请访问:

http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369 http://developer.apple.com/documentation/Cocoa/Conceptual/DataFormatting/Articles/dfDateFormatting10_4.html#//apple_ref/doc/uid/TP40002369

You may have a look to the NSDate and NSCalendar classes. 您可以查看NSDate和NSCalendar类。 For example, here and here 例如, 这里这里

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];

Use NSCalendar and NSDateComponents. 使用NSCalendar和NSDateComponents。 As shown in the NSDateComponents documentation: 如NSDateComponents文档中所示:

NSCalendar *gregorian = [[NSCalendar alloc]
initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *date = [gregorian dateFromComponents:comps];
NSDateComponents *weekdayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:date];
int weekday = [weekdayComponents weekday];

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 day of 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM