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

栏目: IOS · 发布时间: 7年前

内容简介:我想知道是否可以在NSCateendar对象上设置时区,用于从NSDateComponents创建NSDate.我没有尝试过这个,不知道是否可以工作,但这可能值得一试.http://stackoverflow.com/questions/3655616/setting-an-nsdate-to-specific-date-time-and-timezone

我需要将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.我没有尝试过这个,不知道是否可以工作,但这可能值得一试.

http://stackoverflow.com/questions/3655616/setting-an-nsdate-to-specific-date-time-and-timezone


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

New Dark Age

New Dark Age

James Bridle / Verso Books / 2018-7-17 / GBP 16.99

As the world around us increases in technological complexity, our understanding of it diminishes. Underlying this trend is a single idea: the belief that our existence is understandable through comput......一起来看看 《New Dark Age》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具