NSDate - 更改年份值
我需要更改 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是我为出生日期选择设置 UIDatePicker 限制的代码。允许的最大年龄为 100 岁
Here's my code for setting the UIDatePicker limits for a Date Of Birth selection. Max age allowed is 100yrs
看一下
NSCalendar
,特别是components:fromDate:
和dateFromComponents:
方法。Take a look at
NSCalendar
, especiallycomponents:fromDate:
anddateFromComponents:
methods.我根据霍哈给我的指示找到了答案。
I managed to figure the answer with the pointer Hoha gave me.
从 iOS 8 开始,您可以设置特定的日期组件。例如:
Starting in iOS 8 you can set an specific date component. For example: