设置 NSDate 的第一个月
我正在使用通过 NSDateCompoments 递增/递减的 NSDate:
dateComponents = [[NSDateComponents alloc] init];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
...
[dateComponents setDay:1];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:self.date options:0];
[dateComponents setDay:0];
现在,我想设置将 NSDate 设置为该月的第一天(以及一年的第一个月)的日期,但我不这样做知道怎么做了
非常感谢!!!
编辑:
NSDateComponents *dateComponentsSetFirst = [gregorian components:( NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:self.date];
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
requestTy = @"hours";
break;
case 1:
requestTy = @"days";
NSLog (@"avant %@",self.date);
[dateComponents setDay:1];
self.date = [gregorian dateFromComponents:dateComponentsSetFirst];
[dateComponents setDay:0];
NSLog (@"après %@",self.date);
break;
case 2:
requestTy = @"months";
NSLog (@"before %@",self.date);
[dateComponents setMonth:1];
self.date = [gregorian dateFromComponents:dateComponentsSetFirst];
[dateComponents setMonth:0];
NSLog (@"after : %@",self.date);
break;
default:
break;
}
给我
2011-08-17 11:23:34.365 e-mars[20942:207] avant 2011-08-17 09:23:30 GMT
2011-08-17 11:23:34.366 e-mars[20942:207] après 2011-08-16 22:00:00 GMT
当我通过案例 1 时
!当
2011-08-17 11:26:05.747 e-mars[20942:207] before 2011-08-16 22:00:00 GMT
2011-08-17 11:26:05.747 e-mars[20942:207] after : 2011-08-16 22:00:00 GMT
我通过案例 2 时!
I'm using a NSDate that is incremented/decremented thanks to NSDateCompoments:
dateComponents = [[NSDateComponents alloc] init];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:[NSDate date] options:0];
...
[dateComponents setDay:1];
self.date = [gregorian dateByAddingComponents:dateComponents toDate:self.date options:0];
[dateComponents setDay:0];
Now, I want to set the day to set the NSDate to the first of the month (and the first month of the year too), but I dont know how to do
Thanks a lot!!!
EDIT:
NSDateComponents *dateComponentsSetFirst = [gregorian components:( NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:self.date];
switch (self.segmentedControl.selectedSegmentIndex) {
case 0:
requestTy = @"hours";
break;
case 1:
requestTy = @"days";
NSLog (@"avant %@",self.date);
[dateComponents setDay:1];
self.date = [gregorian dateFromComponents:dateComponentsSetFirst];
[dateComponents setDay:0];
NSLog (@"après %@",self.date);
break;
case 2:
requestTy = @"months";
NSLog (@"before %@",self.date);
[dateComponents setMonth:1];
self.date = [gregorian dateFromComponents:dateComponentsSetFirst];
[dateComponents setMonth:0];
NSLog (@"after : %@",self.date);
break;
default:
break;
}
give me
2011-08-17 11:23:34.365 e-mars[20942:207] avant 2011-08-17 09:23:30 GMT
2011-08-17 11:23:34.366 e-mars[20942:207] après 2011-08-16 22:00:00 GMT
when I pass in the case 1 !
and
2011-08-17 11:26:05.747 e-mars[20942:207] before 2011-08-16 22:00:00 GMT
2011-08-17 11:26:05.747 e-mars[20942:207] after : 2011-08-16 22:00:00 GMT
when I pass in the case 2 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有向日期添加组件。您只需从给定日期初始化 dateComponents,设置日和月(如果需要,还可以设置年),然后获取 dateFromComponents。
苹果日期和时间编程指南
You are not adding components to a date. You simply initialize the dateComponents from a given date, set the day and month (and year if you want) and then get the dateFromComponents.
Apple Date and Time Programming Guide