NSDate - 更改年份值

发布于 2024-10-20 14:18:35 字数 391 浏览 6 评论 0原文

我需要更改 NSDate 对象。我基本上做的是改变年份值。

例如:

NSString *someYear = @"2093";
    NSDate *date = [NSDate date]; // Gets the current date.
    ... Create a new date based upon 'date' but with specified year value.

因此,随着“date”从 init 返回 2011-03-06 22:17:50 +0000,我想创建一个包含 2093-03-06 22:17:50 +0000 的日期。

不过,我希望它尽可能在文化上保持中立,因此无论在什么时区,它都可以工作。

谢谢。

I need to change a NSDate object. What I am basically doing is changing the year value.

for example:

NSString *someYear = @"2093";
    NSDate *date = [NSDate date]; // Gets the current date.
    ... Create a new date based upon 'date' but with specified year value.

So with 'date' returning 2011-03-06 22:17:50 +0000 from init, I would like to create a date with 2093-03-06 22:17:50 +0000.

However I would like this to be as culturally neutral as possible, so it will work whatever the timezone.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

绅刃 2024-10-27 14:18:35

这是我为出生日期选择设置 UIDatePicker 限制的代码。允许的最大年龄为 100 岁

    _dateOfBirth.maximumDate = [NSDate date];

    //To limit the datepicker year to current year -100
    NSDate *currentDate = [NSDate date];
    NSUInteger componentFlags = NSYearCalendarUnit;
    NSDateComponents *components = [[NSCalendar currentCalendar] components:componentFlags fromDate:currentDate];
    NSInteger year = [components year];
    NSLog(@"year = %d",year);
    [components setYear:-100];
    NSDate *minDate =  [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate  options:0];
    _dateOfBirth.minimumDate = minDate;

Here's my code for setting the UIDatePicker limits for a Date Of Birth selection. Max age allowed is 100yrs

    _dateOfBirth.maximumDate = [NSDate date];

    //To limit the datepicker year to current year -100
    NSDate *currentDate = [NSDate date];
    NSUInteger componentFlags = NSYearCalendarUnit;
    NSDateComponents *components = [[NSCalendar currentCalendar] components:componentFlags fromDate:currentDate];
    NSInteger year = [components year];
    NSLog(@"year = %d",year);
    [components setYear:-100];
    NSDate *minDate =  [[NSCalendar currentCalendar] dateByAddingComponents:components toDate:currentDate  options:0];
    _dateOfBirth.minimumDate = minDate;
眼眸里的那抹悲凉 2024-10-27 14:18:35

看一下 NSCalendar,特别是 components:fromDate:dateFromComponents: 方法。

Take a look at NSCalendar, especially components:fromDate: and dateFromComponents: methods.

一花一树开 2024-10-27 14:18:35

我根据霍哈给我的指示找到了答案。

NSNumber *newYear = [[NSNumber alloc] initWithInt:[message intValue]];
    NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents* dateComponents = [gregorian components:unitFlags fromDate:[NSDate date]];
    [dateComponents setYear:[newYear intValue]];
    NSDate *newDate = [gregorian dateFromComponents:dateComponents];
    [newYear release];

I managed to figure the answer with the pointer Hoha gave me.

NSNumber *newYear = [[NSNumber alloc] initWithInt:[message intValue]];
    NSCalendar* gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    unsigned int unitFlags = NSYearCalendarUnit | NSDayCalendarUnit | NSMonthCalendarUnit;
    NSDateComponents* dateComponents = [gregorian components:unitFlags fromDate:[NSDate date]];
    [dateComponents setYear:[newYear intValue]];
    NSDate *newDate = [gregorian dateFromComponents:dateComponents];
    [newYear release];
少女七分熟 2024-10-27 14:18:35

从 iOS 8 开始,您可以设置特定的日期组件。例如:

date = [calendar dateBySettingUnit:NSCalendarUnitYear value:year ofDate:date options:0];

Starting in iOS 8 you can set an specific date component. For example:

date = [calendar dateBySettingUnit:NSCalendarUnitYear value:year ofDate:date options:0];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文