如何在目标c中查找一个月中的星期五日期和一个月中的日期

时间:2013-10-30 04:10:31

标签: ios objective-c nsdate nsdateformatter

我正在开发一个使用日期,月份,年份的应用程序。我想要当前的月份日期和当前月份日期,因为当前月份可以有28,29,30,31天。我已经尝试了当年的当月。但我不知道上面的代码。这是我的代码。

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

NSDate           *today           = [NSDate date];
NSCalendar       *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *yearComponents  = [currentCalendar components:NSYearCalendarUnit  fromDate:today];
int currentYear  = [yearComponents year];
for(int months = 0; months < 12; months++)
{
    NSString *monthName = [NSString stringWithFormat:@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear];
    NSLog(@"%@ %i",[[dateFormatter monthSymbols]objectAtIndex: months],currentYear);
    [monthArray addObject:monthName];
}

1 个答案:

答案 0 :(得分:0)

您可以将NSDateComponentsNSCalendar's dateFromComponents结合使用:方法:

尝试类似的事情,

    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *c = [[NSDateComponents alloc] init];
    [c setYear:2013];
    [c setWeekday:6];//1=sun,2=mon .....
    for (int i = 1; i <= 12; i++)
    {
        [c setMonth:i];
        for (int j=1; j<=5; j++)
        {
            [c setWeekdayOrdinal:j];
            NSDate *date = [gregorian dateFromComponents:c];
            NSLog(@"Date[%i]: %@",i,date);

        }

    }