【问题标题】:Display NSDates in terms of week按周显示 NSDates
【发布时间】:2012-11-09 07:30:38
【问题描述】:

场景:

我有一个费用跟踪 iOS 应用程序,我将费用从费用详细视图控制器存储到表视图(带有获取的结果控制器)中,该表视图显示费用列表以及类别、金额和日期。我的实体“Money”中确实有一个日期属性,它是支出或收入的父实体。

问题:

我想要的基本上是对给定一周、一个月或一年的费用进行分类,并将其显示为部分标题标题,例如:(2012 年 10 月 1 日至 10 月 7 日),它显示了费用金额和相关内容根据那个特定的星期。该视图中提供了两个按钮,如果我按下右侧按钮,它将一周增加一周(2012 年 10 月 1 日至 10 月 7 日现在显示 2012 年 10 月 8 日至 10 月 15 日),同样,左侧按钮将减少一周一个星期。

我将如何做到这一点?我正在尝试以下代码 - 不起作用。

- (void)weekCalculation
{
   NSDate *today = [NSDate date];  // present date (current date)

   NSCalendar* calendar = [NSCalendar currentCalendar];
   NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:today];

   [comps setWeekday:1]; // 1: Sunday
   firstDateOfTheWeek = [[calendar dateFromComponents:comps] retain];
   [comps setWeekday:7]; // 7: Saturday
   lastDateOfTheWeek = [[calendar dateFromComponents:comps] retain];

   NSLog(@" first date of week =%@", firstDateOfTheWeek);
   NSLog(@" last date of week =%@", lastDateOfTheWeek);

   firstDateOfWeek = [dateFormatter stringFromDate:firstDateOfTheWeek];
   lastDateOfWeek = [dateFormatter stringFromDate:lastDateOfTheWeek];

}

递增日期代码 -

- (IBAction)showNextDates:(id)sender
{
   int addDaysCount = 7;

   NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
   [dateComponents setDay:addDaysCount];

   NSDate *newDate1 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:firstDateOfTheWeek options:0];

   NSDate *newDate2 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:lastDateOfTheWeek options:0];

   NSLog(@" new dates =%@ %@", newDate1, newDate2);

}

假设星期显示如下(2012 年 11 月 4 日 - 2012 年 11 月 10 日),我按下增量按钮,我在控制台中看到,日期更改为 2012 年 11 月 11 日和 2012 年 11 月 17 日,这是正确的,但如果我按下再次增加按钮,它再次显示相同的日期(2012 年 11 月 11 日和 2012 年 11 月 17 日)。

请帮帮我。

【问题讨论】:

  • 您的 firstDateOfWeek 和 lastDateOfWeek 是字符串而不是日期。在[[NSCalendar currentCalendar] dateByAddingComponents:dateComponents toDate:firstDateOfTheWeek options:0];中使用时,需要转换为日期。
  • @ACB 你读错了。它们只是日期。先生,请仔细阅读。
  • @ACB 你认为这段代码应该把日期增加一周吗,不管增加按钮被按下多少次?
  • 哦,好吧。 firstDateOfWeek 和 lastDateOfTheWeek 看起来令人困惑。在这种情况下,您可以打印 lastDateOfTheWeek 并检查再次递增之前的日期吗?
  • NSDate 今天 = [NSDate 日期]; // 当前日期(当前日期) NSDateComponents comps = [日历组件:NSYear..Unit fromDate:today];所以它总是从今天开始计算,对吧?如果我是正确的,您应该在此处切换到新日期。

标签: ios nsdate nsdateformatter nsdatecomponents


【解决方案1】:

在您的班级中将currentDate 声明为@property。试试这个。

@property(nonatomic, retain) NSDate *currentDate;

初始设置

self.currentDate = [NSDate date];

在调用此方法之前。

- (void)weekCalculation
{
   NSCalendar* calendar = [NSCalendar currentCalendar];
   NSDateComponents* comps = [calendar components:NSYearForWeekOfYearCalendarUnit |NSYearCalendarUnit|NSMonthCalendarUnit|NSWeekCalendarUnit|NSWeekdayCalendarUnit fromDate:self.currentDate];

   [comps setWeekday:1]; // 1: Sunday
   firstDateOfTheWeek = [[calendar dateFromComponents:comps] retain];
   [comps setWeekday:7]; // 7: Saturday
   lastDateOfTheWeek = [[calendar dateFromComponents:comps] retain];

   NSLog(@" first date of week =%@", firstDateOfTheWeek);
   NSLog(@" last date of week =%@", lastDateOfTheWeek);

   firstDateOfWeek = [dateFormatter stringFromDate:firstDateOfTheWeek];
   lastDateOfWeek = [dateFormatter stringFromDate:lastDateOfTheWeek];

}

加载视图后,self.currentDate 的值应从 showNextDates 更新。确保它没有在其他任何地方重置。

- (IBAction)showNextDates:(id)sender
{
   int addDaysCount = 7;

   NSDateComponents *dateComponents = [[[NSDateComponents alloc] init] autorelease];
   [dateComponents setDay:addDaysCount];

   NSDate *newDate1 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:firstDateOfTheWeek options:0];

   NSDate *newDate2 = [[NSCalendar currentCalendar]
                    dateByAddingComponents:dateComponents
                    toDate:lastDateOfTheWeek options:0];

   NSLog(@" new dates =%@ %@", newDate1, newDate2);

   self.currentDate = newDate1;

}

【讨论】:

  • 不,这不起作用。当我第一次按下递增按钮时,日期变为 2012 年 11 月 11 日 - 2012 年 11 月 17 日,没错,但当我再次按下按钮时,它再次显示相同的日期。
  • 星期的第一个日期的 NSLog 中打印什么?你有没有机会休息currentDate?也尝试使用 self.currentDate = newDate2;
  • 它根本不起作用。请参阅此输出: 2012-11-09 00:48:58.650 XpenseTrack[52798:c07] 一周的第一个日期 =2012-11-04 07:00:00 +0000 2012-11-09 00:48:58.650 XpenseTrack[52798 :c07] 一周的最后日期 =2012-11-10 08:00:00 +0000 2012-11-09 00:49:03.678 XpenseTrack[52798:c07] 自当前日期 = 2012-11-11 08:00:00 +0000 2012-11-17 08:00:00 +0000 2012-11-09 00:49:06.206 XpenseTrack[52798:c07] 自当前日期 = 2012-11-11 08:00:00 +0000 2012-11- 17 08:00:00 +0000 2012-11-09 00:49:08.972 XpenseTrack[52798:c07] 自当前日期 = 2012-11-11 08:00:00 +0000 2012-11-17 08:00:00 +0000
  • 按下增量按钮后,它会一遍又一遍地打印相同的日期。
  • “new Dates =”的 NSLog 在哪里?添加 7 后的两个新日期是什么?你能在设置currentDate = newDate1时检查一下,它是否设置了下周的星期日?
【解决方案2】:

我在一个旧项目中需要类似的东西,并通过下面的代码实现了它。它将本周日期设置为 NSDate 变量,并在每个按钮单击中添加/删除 7 天。代码如下:

NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDate *date = [NSDate date];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)  fromDate:date ];
//
[components setDay:([components day] - ([components weekday] )+2)];
self.currentDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss";
self.datelabel.text = [formatter stringFromDate:self.currentDate];

上面的代码计算本周开始并将其设置为 currentDate 变量。我有两个 UIButtons 与 UIActions 命名 prevClick 和 nextClick 调用设置下一个或上一个 weekstart 的方法:

- (IBAction)prevClick:(id)sender {
[self addRemoveWeek:NO];

}

-(void)addRemoveWeek:(BOOL)add{
NSCalendar *calendar = [NSCalendar currentCalendar];
[calendar setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
NSDateComponents *components = [calendar components:(NSYearCalendarUnit|NSWeekdayCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit)  fromDate:self.currentDate ];
components.day = add?components.day+7:components.day-7;
self.currentDate = [calendar dateFromComponents:components];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"dd.MM.yyyy HH:mm:ss";
self.datelabel.text = [formatter stringFromDate:self.currentDate];

}

- (IBAction)nextClk:(id)sender {
[self addRemoveWeek: YES];

}

【讨论】:

  • @Alessandro 您编写的代码基本上在我按下增量按钮的日期上增加了 7 天。我想要的是增量按钮应该将我一周的开始日期和一周的结束日期增加 7 天。就像我的标签显示的那样:(2012 年 10 月 1 日 - 2012 年 10 月 7 日)。在增量按钮操作之后,标签应显示(2012 年 10 月 8 日 - 2012 年 10 月 15 日)。我将如何做到这一点?
  • @AlessandroMinoccheri 我的错。对不起
  • @madiden 您编写的代码基本上在我按下增量按钮的日期上增加了 7 天。我想要的是增量按钮应该将我一周的开始日期和一周的结束日期增加 7 天。就像我的标签显示的那样:(2012 年 10 月 1 日 - 2012 年 10 月 7 日)。在增量按钮操作之后,标签应显示(2012 年 10 月 8 日 - 2012 年 10 月 15 日)。我将如何做到这一点?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-09
  • 2018-02-10
  • 2021-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-07
相关资源
最近更新 更多