如何将日期从 yyyy-MM-dd 格式化为 dd.MM.yyyy

发布于 2024-11-29 09:51:20 字数 716 浏览 0 评论 0原文

我有一个包含此数据的字符串:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不吻晚风 2024-12-06 09:51:20

不要释放

now

这是一个自动释放的对象,您过度释放它,这导致了崩溃。

Don't release

now

This is an autoreleased object, you are over releasing it and this is causing your crash.

爱你是孤单的心事 2024-12-06 09:51:20

您应该始终使用'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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文