如何找到一个月中的特定日期?

Posted

技术标签:

【中文标题】如何找到一个月中的特定日期?【英文标题】:How to find particular day of a month? 【发布时间】:2012-11-29 04:51:19 【问题描述】:

我对 NSDate NSDateFormatterNSCalendar 进行了大量研究,但无法找到找到一个月中特定日期的方法。 例如, 我想找到 12 月的第二个星期一的日期 (10/12/2012)。我知道如何找到周数或月数,但不知道怎么做。

谢谢。

【问题讨论】:

输入是“12 月的第二个星期一”,输出应该是“10/12/2012”。这就是你想要的? How to get NSDate day, month and year in integer format?的可能重复 @ParamasivanSamuttiram 这正是我想要的 【参考方案1】:
    // 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】:

尝试从这些代码中找到一周中的特定日期,然后将其应用于该月:)

请参考以下链接:-

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

Number of days in the current month using iPhone SDK?

希望他们提供帮助:)

【讨论】:

@AdityaMathur 欢迎您......还有什么请告诉我 :)【参考方案3】:

Robin 和 Gill 的回答帮助我找到了解决问题的方法。通过更多的编辑,我能够解决我的问题。

这是我用来获取解决方案的代码

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

现在找到 2012 年 12 月的第三个星期一,调用方法为

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

【讨论】:

以上是关于如何找到一个月中的特定日期?的主要内容,如果未能解决你的问题,请参考以下文章

用 Date 对象查找一个月中的所有日子?

如何在 obiee 中显示一个月中所有日期的一个值?

获取日期差异,然后在其间的每个月中添加 1

如何使用动态数据透视表使一个月中的日期显示为列 - 明确提到月

找出一个月中给定日期的周数

同一天或一个月中的Java日期