在某些情况下,将NSString转换为NSDate会增加一年 [英] Converting NSString to NSDate adds one year in some cases

查看:175
本文介绍了在某些情况下,将NSString转换为NSDate会增加一年的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从年底以来,我将NSString转换为NSDate有些问题。



例如2013-01-05成为2014-01-05转换为NSDate后, 。



因为这是一整年,所以不会像时区欺骗那样。





有没有人知道可能有什么错误?



代码:
NSString * dateString = postInfo.no​​teDate;
NSString * newDateString = [dateString substringToIndex:10];

  NSDateFormatter * dateFormat = [[NSDateFormatter alloc] init] ; 
[dateFormat setDateFormat:@YYYY-MM-dd];
NSDate * date = [[NSDate alloc] init];
date = [dateFormat dateFromString:newDateString];

newDateString返回2013-01-05



date returns 2014-01-05

解决方案

docs


Y 1..n 1997年Week of Yearbased calendar)。通常,长度指定填充,但是对于两个字母,它还指定最大长度。今年的指定用于ISO 8601定义的ISO年周历,但可用于需要周日处理的非基于格雷戈里的日历系统。不一定与日历年的值相同。



y 1..n 1996年。通常,长度指定了填充,但是对于两个字母,它还指定了最大长度。


所以你想要yyyy >

这个错误也在精彩的 WWDC 2011年视频会话117 - 执行日历计算是任何iOS / Cocoa-Developer的必读内容。



维基百科中的 ISO 8601






  NSDate * date = [[NSDate alloc] init]; 
date = [dateFormat dateFromString:newDateString];

您创建一个NSDate,而不是创建另一个并覆盖第一个。只要做

  NSDate * date = [dateFormat dateFromString:newDateString]; 


I have some issues converting an NSString to NSDate since the end of the year. The code have always worked great before, but it suddenly started to behave wierd...

For example 2013-01-05 becomes 2014-01-05 when converted to NSDate.

Since it's a whole year it doesn't feel like it's the timezone spooking.

It's not doing this with dates from 2012.

Does anybody have an idea of what might be wrong?

Code: NSString *dateString = postInfo.noteDate; NSString *newDateString = [dateString substringToIndex:10];

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init]; 
    [dateFormat setDateFormat:@"YYYY-MM-dd"]; 
    NSDate *date = [[NSDate alloc] init];
    date = [dateFormat dateFromString:newDateString];

newDateString returns 2013-01-05

date returns 2014-01-05

解决方案

From the docs:

Y 1..n 1997 Year (in "Week of Year" based calendars). Normally the length specifies the padding, but for two letters it also specifies the maximum length. This year designation is used in ISO year-week calendar as defined by ISO 8601, but can be used in non-Gregorian based calendar systems where week date processing is desired. May not always be the same value as calendar year.

y 1..n 1996 Year. Normally the length specifies the padding, but for two letters it also specifies the maximum length.

So you want 'yyyy'

This 'bug' is also discussed in the fantastic WWDC 2011 Video "Session 117 - Performing Calendar Calculations", a must-see for any iOS/Cocoa-Developer.

Wikipedia article on ISO 8601


NSDate *date = [[NSDate alloc] init];
date = [dateFormat dateFromString:newDateString];

You create a NSDate and than you create another and overwrite the first one. Just do

NSDate *date = [dateFormat dateFromString:newDateString];

这篇关于在某些情况下,将NSString转换为NSDate会增加一年的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆