NSDate格式和值的奇怪行为 [英] NSDate format and value strange behavior
问题描述
我有以下代码:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
myres.start_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]];
NSLog(@"Start date is %@", [NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]);
NSLog(@"Start date from NSDate is %@", myres.start_date);
myres.end_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"end_time"]]];
NSLog(@"End date is %@", [NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"end_time"]]);
NSLog(@"End date from NSDate is %@", myres.end_date);
我得到的结果是:
Start date is 2011-03-31 9:00:00
Start date from NSDate is 2011-03-31 16:00:00 +0000
End date is 2011-03-31 10:00:00
End date from NSDate is 2011-03-31 17:00:00 +0000
为什么结果不同? 我想拥有它,以便当我调用myres.start_date描述时,它为我提供与字符串相同的数据.
结果有所不同,因为您正在使用通用的description
方法打印NSDate
实例,该方法与您期望的输出格式不匹配. /p>
如果使用[df stringFromDate:myres.start_date]
,则应该获得相同的输出.
请记住,NSDate
存储时间的表示形式,并且有许多种打印时间的方式,并且description
的打印时间方式是固定的
NSDate
的description
唯一返回所需格式的字符串的唯一方法是使用类别覆盖它或使用子类.
您似乎对时间的存储和显示方式有一个基本的误解:您的NSDate
存储的是NSDateFormatter
解析的时间,实际上它返回的是相同的 data 其description
方法.它只是以不同的形式显示.
I have the following code:
NSDateFormatter * df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
[df setFormatterBehavior:NSDateFormatterBehaviorDefault];
myres.start_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]];
NSLog(@"Start date is %@", [NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"start_time"]]);
NSLog(@"Start date from NSDate is %@", myres.start_date);
myres.end_date = [df dateFromString:[NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"end_time"]]];
NSLog(@"End date is %@", [NSString stringWithFormat:@"%@ %@", [self.data objectForKey:@"date"], [self.data objectForKey:@"end_time"]]);
NSLog(@"End date from NSDate is %@", myres.end_date);
and the result I am getting is:
Start date is 2011-03-31 9:00:00
Start date from NSDate is 2011-03-31 16:00:00 +0000
End date is 2011-03-31 10:00:00
End date from NSDate is 2011-03-31 17:00:00 +0000
why is the result different? I would like to have it so that when I call myres.start_date description, it gives me the same data as the string.
The result is different because you are printing your NSDate
instances using the generic description
method, which doesn't match the output format you expect.
If you used [df stringFromDate:myres.start_date]
you should get the same output.
Keep in mind that NSDate
stores a representation of time, and there are many ways to print time and description
's way of printing time is fixed.
The only way to have description
of NSDate
return a string in the format you want is to override it with a category or use a subclass.
You seem to have a fundamental misunderstanding of how time is stored and displayed: your NSDate
is storing the time parsed by NSDateFormatter
, and it is in fact returning out the same data in its description
method. It is just displaying it in a different form.
这篇关于NSDate格式和值的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!