设置 NSDate 的第一个月

发布于 2024-11-29 17:38:13 字数 1994 浏览 1 评论 0原文

我正在使用通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2024-12-06 17:38:13

您没有向日期添加组件。您只需从给定日期初始化 dateComponents,设置日和月(如果需要,还可以设置年),然后获取 dateFromComponents。
苹果日期和时间编程指南

NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[NSDate date]];
[dateComponents setDay:1];
[dateComponents setMonth:1];
self.date = [gregorian dateFromComponents:dateComponents];

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

NSDateComponents *dateComponents = [gregorian components:( NSYearCalendarUnit |
NSMonthCalendarUnit | NSDayCalendarUnit ) fromDate:[NSDate date]];
[dateComponents setDay:1];
[dateComponents setMonth:1];
self.date = [gregorian dateFromComponents:dateComponents];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文