加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

iphone – 将NSDate设置为特定的日期,时间和时区

发布时间:2020-12-15 01:38:33 所属栏目:百科 来源:网络整理
导读:我需要将NSDate设置为iPhone App的特定日期,时间和时区. 下面的示例代码在iOS 4上可以正常工作,因为setTimeZone与iOS 4一起引入.如何在不使用setTimeZone的情况下轻松将NSDate设置为日期,时间和时区,以便它可以在iOS 3.0设备上使用? NSDateComponents *comp
我需要将NSDate设置为iPhone App的特定日期,时间和时区.

下面的示例代码在iOS 4上可以正常工作,因为setTimeZone与iOS 4一起引入.如何在不使用setTimeZone的情况下轻松将NSDate设置为日期,时间和时区,以便它可以在iOS 3.0设备上使用?

NSDateComponents *comps = [[NSDateComponents alloc] init];
[comps setYear:2010];
[comps setMonth:8];
[comps setDay:24];
[comps setHour:17];
[comps setMinute:5];
[comps setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"] ];

NSCalendar *cal = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDate *referenceTime = [cal dateFromComponents:comps];

猜猜这应该很简单,但不知何故NSDate,组件和日历和我不是好朋友…

解决方法

我想知道是否可以在NSCateendar对象上设置时区,用于从NSDateComponents创建NSDate.我没有尝试过这个,不知道是否可以工作,但这可能值得一试.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读