如何更改日期/时间的格式? [英] How to change format of date/time?

查看:136
本文介绍了如何更改日期/时间的格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种日期和时间格式:

I have this date and time format:

2010-05-19 07:53:30

并希望将其更改为:

Wednesday @ 7:53PM 5/19/2010

,它获得当前的格式:

I'm doing this, which gets the current format:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";

但是当我改变格式时,我最终得到一个null。例如:

but when I change the format, I end up with a null. For example:

formatter.dateFormat = @"hh:mm tt MM-dd-yyyy";
date = [formatter stringFromDate:formattedDate];

日期将为null。我想把最终结果放入NSString。这将是很好,如果时间和日期可以作为单独的属性,所以我可以安排他们,但我喜欢。有关如何更改格式的任何想法?

date will be null. I want to put the end result into an NSString. It would be nice if time and date could come out as separate properties so I can arrange them however I like. Any ideas on how I can change the formatting?

推荐答案

我认为你的格式化字符串是问题。您只能使用 UTS#35日期格式模式中的表格中找到的字符。我试过你的代码,当时间 hh:mm 显示正确,格式化停止在 tt - 不在表中!

I think your formatting string is the problem. You should only use the characters you find in the table in UTS#35 Date Format Patterns. I tried your code and while the time hh:mm displays correctly, formatting stops at tt - not in the table!

如果你真的想要格式字符串中不在表格中的字符,你可以转义它们,例如 hh'o''clock'a ,zzzz - 产生格式如12点PM,太平洋夏令时。

If you really want characters in the format string that are not in the table you can escape them, like hh 'o''clock' a, zzzz - produces format like "12 o'clock PM, Pacific Daylight Time".

这篇关于如何更改日期/时间的格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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