2 daffodilgirl DaffodilGirl 于 2012.11.30 13:30 提问

如何查询一个月中特定的一天?

研究了很久NSDateNSDateFormatterNSCalendar,不知道怎么实现找出一月个中指定一天的功能。比如查询十二月第二个星期一的日期是多少,不知道怎么实现呢?

谢谢指教。

2个回答

DaffodilGirl
DaffodilGirl   2012.11.30 16:53
已采纳

谢谢回答者的答案,我根据得到的回答,实现了。最后这样的

    -(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;
}

现在如果查询十二月的第三个星期一调用方法:

  [self findWeekDay:2 forWeek:3 OfMonth:12 OfYear:2012];
Chengzi_963
Chengzi_963   2012.11.30 14:36
// 设置月份中开始的时间
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];
NSInteger weekday = [weekdayComponents weekday];
int weekNumber = 2;
int dayOfweek = 2;
int x = (7 - weekday) * (weekNumber - 1) + dayOfweek;
// 添加第几个星期
if(x < 7)
{
    x += 7 * (weekNumber - 1) + 1;
}
else
{
    x += 7 * (weekNumber - 2) + 1;
}
DaffodilGirl
DaffodilGirl 谢谢你的回答,我根据你的回答自己写出来了实现代码
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!