如何找到一个月的某一天?

时间:2022-12-27 19:21:15

I've done a lot of study on NSDate NSDateFormatter and NSCalendar but cannot figure out a way to find a particular day of a month. For example, I want to find the date of 2nd Monday of December (10/12/2012). I know how to find number of week or number of month but cannot figure out the way to do it.

我在NSDate NSDateFormatter和NSCalendar上做了很多研究,但是我不知道如何找到一个月中的某一天。例如,我想找到12月2日星期一的日期(10/12/2012)。我知道怎么找到星期几或一个月几,但是我不知道怎么做。

Thanks.

谢谢。

3 个解决方案

#1


3  

    // Set start date of the month you want
    NSString *str = @"01 - 12 - 2012";
    NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
    [formatter1 setDateFormat:@"dd - MM - yyyy"];
    NSDate *date = [formatter1 dateFromString:str];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    // Get the weekday with sunday as one
    NSInteger weekday = [weekdayComponents weekday];
    int weekNumber = 2;
    int dayOfweek = 2;
    int x = (7 - weekday) * (weekNumber - 1) + dayOfweek;
    // Add the no. of weeks - 1 or - 2 depending on the value of x.
    // Also Add 1 as the start date is 1st Dec.
    if(x < 7)
    {
        x += 7 * (weekNumber - 1) + 1;
    }
    else
    {
        x += 7 * (weekNumber - 2) + 1;
    }

#2


1  

Try to find the particular day of the week from these codes then apply it for the month as well :)

试着从这些代码中找出一周中的某一天,然后把它应用到这个月。

Please refer to the following links :-

请参阅以下连结:-

How do I get the day of the week with Cocoa Touch?

我如何获得可可触觉的一天?

Number of days in the current month using iPhone SDK?

使用iPhone SDK的当月天数?

Hope They Help :)

希望他们帮助:)

#3


0  

Robin's and Gill's answers helped me get a solution to my problem. With some more editing I was able to get a solution to my problem.

罗宾和吉尔的回答帮助我找到了解决问题的办法。通过更多的编辑,我找到了解决问题的办法。

Here is the code that I used to get the solution

这是我用来得到解的代码。

    -(void)findWeekDay:(NSInteger)weekDay forWeek:(NSInteger)week OfMonth:(NSInteger)month OfYear:(NSInteger)year
{
        NSDateComponents *dateComp = [[NSDateComponents alloc]init];
[dateComp setYear:year];
[dateComp setMonth:month];
[dateComp setDay:1];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *firstDate=[gregorian dateFromComponents:dateComp];  //Sets first date of month.
firstDate=[self dateToGMT:firstDate];
NSLog(@"First date: %@",firstDate);
NSDateComponents *dateComp1 = [gregorian components:NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:firstDate];
NSInteger firstWeekDay=[dateComp1 weekday];  //Gets firstday of a week. 1-Sunday, 2-Monday and so on.
NSLog(@"First Week Day: %d",firstWeekDay);
NSRange daysInMonth = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDate];   //To get number of days in that month
NSInteger x;    //days to be added to first date
if (firstWeekDay < weekDay) {
    x = (weekDay - firstWeekDay) + (7 * (week-1));
}
if (firstWeekDay > weekDay) {
    x = (7 - firstWeekDay + weekDay) + (7 * (week-1));
}
if (firstWeekDay == weekDay) {
    x = (7 * (week-1));
}
if (x > daysInMonth.length) {
    NSLog(@"Invalid Date: %d",x);
}
else {
    NSLog(@"Days to be added: %d",x);
    NSDateComponents *dateComp2 = [[NSDateComponents alloc]init];
    [dateComp2 setYear:0];
    [dateComp2 setMonth:0];
    [dateComp2 setDay:x];
    NSDate *desiredDate = [gregorian dateByAddingComponents:dateComp2 toDate:firstDate options:0];
    NSLog(@"Your desired date is: %@",desiredDate);
}

}

}

    - (NSDate *)dateToGMT:(NSDate *)sourceDate {
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate];
return destinationDate;

}

}

Now to find 3rd Monday of December 2012 call the method as

现在找到2012年12月的第3个星期一调用方法as

    [self findWeekDay:2 forWeek:3 OfMonth:12 OfYear:2012];

#1


3  

    // Set start date of the month you want
    NSString *str = @"01 - 12 - 2012";
    NSDateFormatter *formatter1 = [[[NSDateFormatter alloc] init] autorelease];
    [formatter1 setDateFormat:@"dd - MM - yyyy"];
    NSDate *date = [formatter1 dateFromString:str];
    NSCalendar *gregorian = [[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] autorelease];
    NSDateComponents *weekdayComponents = [gregorian components:(NSDayCalendarUnit | NSWeekdayCalendarUnit) fromDate:date];
    // Get the weekday with sunday as one
    NSInteger weekday = [weekdayComponents weekday];
    int weekNumber = 2;
    int dayOfweek = 2;
    int x = (7 - weekday) * (weekNumber - 1) + dayOfweek;
    // Add the no. of weeks - 1 or - 2 depending on the value of x.
    // Also Add 1 as the start date is 1st Dec.
    if(x < 7)
    {
        x += 7 * (weekNumber - 1) + 1;
    }
    else
    {
        x += 7 * (weekNumber - 2) + 1;
    }

#2


1  

Try to find the particular day of the week from these codes then apply it for the month as well :)

试着从这些代码中找出一周中的某一天,然后把它应用到这个月。

Please refer to the following links :-

请参阅以下连结:-

How do I get the day of the week with Cocoa Touch?

我如何获得可可触觉的一天?

Number of days in the current month using iPhone SDK?

使用iPhone SDK的当月天数?

Hope They Help :)

希望他们帮助:)

#3


0  

Robin's and Gill's answers helped me get a solution to my problem. With some more editing I was able to get a solution to my problem.

罗宾和吉尔的回答帮助我找到了解决问题的办法。通过更多的编辑,我找到了解决问题的办法。

Here is the code that I used to get the solution

这是我用来得到解的代码。

    -(void)findWeekDay:(NSInteger)weekDay forWeek:(NSInteger)week OfMonth:(NSInteger)month OfYear:(NSInteger)year
{
        NSDateComponents *dateComp = [[NSDateComponents alloc]init];
[dateComp setYear:year];
[dateComp setMonth:month];
[dateComp setDay:1];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *firstDate=[gregorian dateFromComponents:dateComp];  //Sets first date of month.
firstDate=[self dateToGMT:firstDate];
NSLog(@"First date: %@",firstDate);
NSDateComponents *dateComp1 = [gregorian components:NSDayCalendarUnit|NSWeekdayCalendarUnit fromDate:firstDate];
NSInteger firstWeekDay=[dateComp1 weekday];  //Gets firstday of a week. 1-Sunday, 2-Monday and so on.
NSLog(@"First Week Day: %d",firstWeekDay);
NSRange daysInMonth = [gregorian rangeOfUnit:NSDayCalendarUnit inUnit:NSMonthCalendarUnit forDate:firstDate];   //To get number of days in that month
NSInteger x;    //days to be added to first date
if (firstWeekDay < weekDay) {
    x = (weekDay - firstWeekDay) + (7 * (week-1));
}
if (firstWeekDay > weekDay) {
    x = (7 - firstWeekDay + weekDay) + (7 * (week-1));
}
if (firstWeekDay == weekDay) {
    x = (7 * (week-1));
}
if (x > daysInMonth.length) {
    NSLog(@"Invalid Date: %d",x);
}
else {
    NSLog(@"Days to be added: %d",x);
    NSDateComponents *dateComp2 = [[NSDateComponents alloc]init];
    [dateComp2 setYear:0];
    [dateComp2 setMonth:0];
    [dateComp2 setDay:x];
    NSDate *desiredDate = [gregorian dateByAddingComponents:dateComp2 toDate:firstDate options:0];
    NSLog(@"Your desired date is: %@",desiredDate);
}

}

}

    - (NSDate *)dateToGMT:(NSDate *)sourceDate {
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:destinationGMTOffset sinceDate:sourceDate];
return destinationDate;

}

}

Now to find 3rd Monday of December 2012 call the method as

现在找到2012年12月的第3个星期一调用方法as

    [self findWeekDay:2 forWeek:3 OfMonth:12 OfYear:2012];