- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我用“DateFromComponents”设置了一个日期,当我读出它时......它是错误的。我必须将日期设置为第二天或以后的任何时间,这样我才能得到正确的年份?!?我设置了 2011 年,当我读出它时,我得到了 2010 年!!
<小时/> day = 1;
month = 1;
year = 2011;
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
NSDate *date = [gregorian dateFromComponents:components];
[gregorian release];
NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"DD MMMM YYYY"];
NSLog (@"hmm: %@",[dateFormatter stringFromDate:actDate]);
-------- 这里我得到1 一月 2010///2010 而不是 2011
!?!?
如何解决这个问题。
谢谢克里斯
最佳答案
我运行了这段代码:
NSInteger day = 1;
NSInteger month = 1;
NSInteger year =0;
NSCalendar *gregorian;
NSDateComponents *components;
NSDate *theDate;
NSDateFormatter *dateFormatter;
for (int i=2005; i<2015; i++) {
year= i;
gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
components = [[NSDateComponents alloc] init];
[components setDay:day];
[components setMonth:month];
[components setYear:year];
theDate = [gregorian dateFromComponents:components];
[gregorian release];
dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"DD MMMM YYYY"];
NSLog(@"year=%d",year);
NSLog (@"hmm: %@",[dateFormatter stringFromDate:theDate]);
NSLog(@"theDate=%@\n\n",theDate);
}
...并得到以下输出:
year=2005
hmm: 01 January 2004
theDate=2005-01-01 00:00:00 -0600
year=2006
hmm: 01 January 2006
theDate=2006-01-01 00:00:00 -0600
year=2007
hmm: 01 January 2007
theDate=2007-01-01 00:00:00 -0600
year=2008
hmm: 01 January 2008
theDate=2008-01-01 00:00:00 -0600
year=2009
hmm: 01 January 2008
theDate=2009-01-01 00:00:00 -0600
year=2010
hmm: 01 January 2009
theDate=2010-01-01 00:00:00 -0600
year=2011
hmm: 01 January 2010
theDate=2011-01-01 00:00:00 -0600
year=2012
hmm: 01 January 2012
theDate=2012-01-01 00:00:00 -0600
year=2013
hmm: 01 January 2013
theDate=2013-01-01 00:00:00 -0600
year=2014
hmm: 01 January 2014
theDate=2014-01-01 00:00:00 -0600
显然问题出在格式化程序而不是日期。但是,我不知道格式化程序有什么问题。 。
编辑:
转移:
[dateFormatter setDateFormat:@"DD MMMM YYYY"];
...至:
[dateFormatter setDateFormat:@"DD MMMM yyyy"];
...解决了问题,尽管我不知道为什么。
关于iPhone NSDateComponent 日期错误 1.1.2011?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3220176/
我正在创建一个每周日历 View ,其中一部分是计算所显示的一周的第一天。我还希望保存的日期具有第一秒的时间,因此我试图确保日期的 hh:mm:ss 均为 00:00:00。 我有以下代码,在大多数情
我正在编写一些代码来获取服务器上的图像。图像文件名采用 yyyyMMdd_HHmm 格式(UTC),每 3 小时生成一次(1200,1500,1800,..等)。当代码启动时,我获取当前 UTC 日期
当我到达 2011 年 11 月 6 日时,我似乎无法弄清楚为什么这一天没有改变。我所做的只是遍历这些日子。任何帮助将不胜感激。这是我的代码: NSCalendar* calendar = [[NSC
我已经从Zaph阅读以下答案 iPhone - Correct way for getting current date and time for a given place / timezone a
如果您要运行此代码: // January 16th 2015 10:20 AM in Amsterdam var date = NSDate(timeIntervalSince1970: 14214
我有这段代码 NSDateComponents *comps = [[NSCalendar currentCalendar] components:(NSCalendarUnitYear | NSCa
我正在使用多个 NSDateComponents 来显示一个时钟,其各个值在整个屏幕上被打散,但我遇到了格式化问题。 秒数显示为 1-60 而不是 00-59,小时数为 24 小时制而不是 12 小时
我有以下代码。当我在我的 iOS 3.1.3 设备上运行它时(克服它!)它按预期工作,但是当我在模拟器(iOS 5 和 4.3)上运行它时它会关闭 1 分 15 秒(9:01:15 ).这是因为我的代
我正在尝试获取两个日期之间的差异,它应该以天为单位显示。 NSLog(@"%@", [NSDate date]); NSDateFormatter *formatter = [[NSDateForma
我正在创建一个 NSDate。为什么一天总是少一天? 这是代码: NSDate *today = [NSDate date]; NSCalendar *gregorian = [[NSCalendar
为什么这给我错误的日期? NSCalendar *myCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCa
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我正在尝试从 2 个变量获取数据:hours = "08" 和 minutes = 30。我是这样做的: let calendar = NSCalendar(identifier: NSCalenda
我的问题是处理下面代码片段中给出的 NSDateComponents 操作的时间配置文件。 我有一个迭代大量 NSDate 对象的方法 该方法的一部分要求我放弃每个日期的小时、分钟和秒数。 为此,我有
我试图在 NSDateComponents 中设置时间,我编写了以下代码 NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIden
我不明白为什么我不能使用 NSDateComponents 正确地更改 NSDate 的小时,我会这样做: NSDateComponent components = [calendar compone
一年有多少周?因为我在网上搜索过,我发现一年总共有 52.1 周,但是如果我在 Xcode 中这样做: NSCalendar *gregorian = [[NSCalendar alloc]
我希望显示来自 NSDate 对象的月数。 //Make Date Six Months In The Future NSCalendar *calendar = [[NSCalendar alloc
如果我有两个 nsdatecomponent 并且我想知道第一个对象中的 TIME 是否大于第二个。 例子: if (nsdatecomponentObject1 > nsdatecomponentO
我一直在使用 Swift 和 Xcode 6.3.2 开发一个时钟应用程序,以下代码构建和运行得很好。 // Get current time let date = NSDate() let cale
我是一名优秀的程序员,十分优秀!