Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以周为单位显示日期_Ios_Nsdate_Nsdateformatter_Nsdatecomponents - Fatal编程技术网

Ios 以周为单位显示日期

Ios 以周为单位显示日期,ios,nsdate,nsdateformatter,nsdatecomponents,Ios,Nsdate,Nsdateformatter,Nsdatecomponents,情景: 我有一个费用跟踪iOS应用程序,我正在将费用从费用详细信息视图控制器存储到一个表视图(带有“获取结果”控制器),该表视图显示费用列表以及类别、金额和日期。我的实体“Money”中有一个日期属性,它是费用或收入的母实体 问题: 我想要的是基本上对我在给定的一周、一个月或一年的费用进行分类,并将其显示为部分标题,例如:(2012年10月1日至10月7日),它根据特定的一周显示费用金额和相关内容。该视图中提供了两个按钮,如果我按下右键,它将增加一周(2012年10月1日至10月7日现在显示20

情景:

我有一个费用跟踪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日)


请在这里帮助我。

我在我的一个老项目中需要类似的东西,并通过下面的代码实现了它。它将此weeks date设置为NSDate变量,并在每次单击按钮时从day组件中添加/删除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变量。我有两个UIButton,其UIActions名为prevClick和nextClick,它调用设置下一周或上一周开始的方法:

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

}


}

在我的一个老项目中,我需要类似的东西,并通过下面的代码实现了它。它将此weeks date设置为NSDate变量,并在每次单击按钮时从day组件中添加/删除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变量。我有两个UIButton,其UIActions名为prevClick和nextClick,它调用设置下一周或上一周开始的方法:

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

}


}

currentDate
声明为类中的
@属性。试试这个

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

}

currentDate
声明为类中的
@属性。试试这个

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

}

您的firstDateOfWeek和lastDateOfWeek是字符串,而不是日期。在[[NSCalendar currentCalendar]dateByAddingComponents:dateComponents toDate:FirstDateOfWeek选项:0]中使用时;,您需要转换为日期。@ACB您读错了。它们只是约会。先生,请仔细阅读。@ACB您认为这段代码是否应该将日期增加一周,无论按了多少次递增按钮?哦,好的。firstDateOfWeek和LastDateOfWeek看起来很混乱。在这种情况下,您可以打印LastDateOfWeek并检查再次递增之前的日期吗?NSDate today=[NSDate date];//当前日期(当前日期)NSDateComponents comps=[日历组件:NSYear..Unit fromDate:today];所以它总是从今天算起,对吗?如果我是正确的,您应该在这里切换到新日期。您的firstDateOfWeek和lastDateOfWeek是字符串,而不是日期。在[[NSCalendar currentCalendar]dateByAddingComponents:dateComponents toDate:FirstDateOfWeek选项:0]中使用时;,您需要转换为日期。@ACB您读错了。它们只是约会。先生,请仔细阅读。@ACB您认为这段代码是否应该将日期增加一周,无论按了多少次递增按钮?哦,好的。firstDateOfWeek和LastDateOfWeek看起来很混乱。在这种情况下,您可以打印LastDateOfWeek并检查再次递增之前的日期吗?NSDate today=[NSDate date];//当前日期(当前日期)NSDateComponents comps=[日历组件:NSYear..Unit fromDate:today];所以它总是从今天算起,对吗?如果我是对的,你应该在这里切换到新的日期。不,这不起作用。当我第一次按下递增按钮时,日期变为2012年11月11日至2012年11月17日,没错,但当我第二次再次按下按钮时,它再次显示相同的日期。NSLog中打印的第一周日期是什么?你有没有可能休息一下?还可以尝试使用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+0000It在按下递增按钮后反复打印相同的日期。“新日期”的NSLog在哪里?添加7后的两个新日期是什么?设置currentDate=newDate1时,您能检查一下吗?是否设置了下周的星期日?不,这不起作用。当我第一次按下递增按钮时,日期变为2012年11月11日至2012年11月17日,没错,但当我再次按下递增按钮时