【问题标题】:Get weeks date in IOS SDK在 IOS SDK 中获取星期日期
【发布时间】:2013-06-28 06:54:53
【问题描述】:

我想用日期名称获取当前和下周日期。

即如果今天的日期是 28-06-2013(星期五),那么我想将日期从 23-06-2013(星期日)到 29-06-2013(星期六)作为本周。

下周 我想获取从 30-06-2013(星期日)到 06-07-2013(星期六)的日期。

我该怎么做?

谢谢,

【问题讨论】:

  • 你想要这个有限的日期在 UIDatepicker 还是你只是想要任何其他功能的日期?
  • @wesley,我没有使用 datePicker。

标签: ios calendar nscalendar


【解决方案1】:

这里是可以解决问题的示例代码。

-(NSArray*)daysThisWeek
{
     return  [self daysInWeek:0 fromDate:[NSDate date]];
}

-(NSArray*)daysNextWeek
{
    return  [self daysInWeek:1 fromDate:[NSDate date]];
}
-(NSArray*)daysInWeek:(int)weekOffset fromDate:(NSDate*)date
{
    NSCalendar *calendar = [NSCalendar currentCalendar];

    //ask for current week
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    comps=[calendar components:NSWeekCalendarUnit|NSYearCalendarUnit fromDate:date];
    //create date on week start
    NSDate* weekstart=[calendar dateFromComponents:comps];
    if (weekOffset>0) {
        NSDateComponents* moveWeeks=[[NSDateComponents alloc] init];
        moveWeeks.week=1;
        weekstart=[calendar dateByAddingComponents:moveWeeks toDate:weekstart options:0];
    }

    //add 7 days
    NSMutableArray* week=[NSMutableArray arrayWithCapacity:7];
    for (int i=1; i<=7; i++) {
        NSDateComponents *compsToAdd = [[NSDateComponents alloc] init];
        compsToAdd.day=i;
        NSDate *nextDate = [calendar dateByAddingComponents:compsToAdd toDate:weekstart options:0];
        [week addObject:nextDate];

    }
    return [NSArray arrayWithArray:week];
}

【讨论】:

  • 感谢您的回复....太好了!有用。但是我怎样才能得到下周的日期呢?
  • 我怎样才能得到这一天(即周日、周一等)?我尝试了很多,但总是给我第二天(即打印星期一而不是星期日),比如 2013-06-30 18:30:00 +0000 日期。
【解决方案2】:

我打算复制并粘贴一个答案,但我建议你去阅读这篇文章:Current Week Start and End Date。看起来它会帮助你实现你想要的。

可能是这样的:

NSDate *now = [NSDate date];

NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger weekNumber =  [[calendar components: NSWeekCalendarUnit fromDate:now] week];

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

NSDateComponents *comp = [gregorian components:NSYearCalendarUnit fromDate:now];
[comp setWeek:weekNumber];  //Week number.

int i;

for (i = 1; i < 8; i=i+1){

    [comp setWeekday:i]; //First day of the week. Change it to 7 to get the last date of the week

    NSDate *resultDate = [gregorian dateFromComponents:comp];

    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"EEEE MMMM d, yyyy"];

    NSString *myDateString = [formatter stringFromDate:resultDate];
    NSLog(@"%@",myDateString);

}

【讨论】:

  • 一旦有了开始和结束日期,您就可以遍历并填写空白。
  • 更新了我的答案以包含解决方案。这将为您提供一周中的所有 7 个日期。
【解决方案3】:

使用NSDateComponents 找出星期几,然后找出它之前和之后所需的日期。

【讨论】:

  • 检查@HeftyHijacker 答案中的链接以获取适当的代码。
【解决方案4】:

嗯,我不是这方面或任何方面的专家,但一种愚蠢的做法是首先能够从NSDate 识别星期几。然后您可以添加 if 语句,然后 if 语句可以添加和减去日期以及 nslogprintf 每个日期。或者我认为有一种方法可以访问 ios 日历。

好的,所以上面说的和做的比我说的要多。是的,去查看我之前帖子上的链接。

【讨论】:

    【解决方案5】:

    使用 NSCalendar 实例将您的起始 NSDate 实例转换为 NSDateComponents 实例,将 1 天添加到 NSDateComponents 并使用 NSCalendar 将其转换回 NSDate 实例。重复最后两个步骤,直到到达结束日期。

    NSDate *currentDate = [NSDate date];
     NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
     NSDateComponents *comps = [[NSDateComponents alloc] init];
     [comps setDay:1];
     NSDate *nextDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    

    否则查找开始日期和结束日期。喜欢

    NSDate *startDate = [NSDate date];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSDateComponents *comps = [[NSDateComponents alloc] init];
    [comps setDay:7];
     NSDate *endDate = [calendar dateByAddingComponents:comps toDate:currentDate options:0];
    

    那么, NSDate nextDate;

    for ( nextDate = startDate ; [nextDate compare:endDate] < 0 ; nextDate = [nextDate addTimeInterval:24*60*60] ) {
      // use date
    }
    

    【讨论】:

    • 您的代码获取当前日期之后的 7 天(从现在起接下来的 7 天),这不是 OP 要求的
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多