如何将日期从 yyyy-MM-dd 格式化为 dd.MM.yyyy
我有一个包含此数据的字符串:
2011-09-13 22:20:00
我想要有这个数据:
13.09.2011
另外,当日期是今天时,我想要有字符串“Today”。
如何在 Objective C 中做到这一点? 我在过去的 1 小时里尝试过,但没有得到它:
//Date basteln
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString* str = [[ens objectForKey:@"datum_server"] substringToIndex:10];
NSLog(@"%@", str);
NSDate *now = [dateFormat dateFromString:str];
NSLog(@"%@", now);
[dateFormat setDateFormat:@"dd.MM.yyyy"];
NSString *theDate = [dateFormat stringFromDate:now];
[dateFormat release];
[now release];
所以我尝试减少时间,将错误的格式转换为正确的格式。 但它在 SIGBART 中崩溃了。
有想法吗?
I have a string with this data:
2011-09-13 22:20:00
I want to have this data:
13.09.2011
Additional I want to have the string "Today" when the date is today.
How to do this in Objective C?
I tried it the last 1 hour, but don't get it:
//Date basteln
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
NSString* str = [[ens objectForKey:@"datum_server"] substringToIndex:10];
NSLog(@"%@", str);
NSDate *now = [dateFormat dateFromString:str];
NSLog(@"%@", now);
[dateFormat setDateFormat:@"dd.MM.yyyy"];
NSString *theDate = [dateFormat stringFromDate:now];
[dateFormat release];
[now release];
So I tried to cut of the time, convert it with the wrong format to the right format.
But it crashed in a SIGBART.
Ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要释放
这是一个自动释放的对象,您过度释放它,这导致了崩溃。
Don't release
This is an autoreleased object, you are over releasing it and this is causing your crash.
您应该始终使用'init'、'copy'、'alloc'、'new'、'copy'或'mutableCopy'来释放那些您拥有的对象。所以你不需要释放日期对象,因为你不拥有它。
You should always release those object which you own it by using 'init', 'copy' 'alloc', 'new', 'copy', or 'mutableCopy'. so there you do not need to release the date object because you did not own that.