如何获得约会和约会时间采用 (MM/dd/yyyy hh:MM a) 格式?

发布于 2024-12-10 14:37:33 字数 480 浏览 4 评论 0原文

在我的应用程序中,我必须显示日期和日期时间就这样,

2011年10月19日下午3点

目前我编写此代码,

NSDateFormatter *formatar=[[NSDateFormatter alloc] init];       
[formatar setDateFormat:@"MM/dd/YYYY   HH:mm a"];
[formatar setTimeStyle:NSDateFormatterShortStyle];
NSString *st=[formatar stringFromDate:[NSDate date]];

但以这种格式获取日期:

2011年10月19日15点

那么,我怎样才能以这种 10/19/2011 3pm 格式获得它?

In my app I have to show Date & Time Like this,

10/19/2011 3pm

Currently I write this code,

NSDateFormatter *formatar=[[NSDateFormatter alloc] init];       
[formatar setDateFormat:@"MM/dd/YYYY   HH:mm a"];
[formatar setTimeStyle:NSDateFormatterShortStyle];
NSString *st=[formatar stringFromDate:[NSDate date]];

But Getting date in this format:

10/19/2011 15.00

So, How can i get that in this 10/19/2011 3pm Format?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

も让我眼熟你 2024-12-17 14:37:33

您需要对日期格式进行一些细微的更改,以使其按照您的需要工作。

1.您的日期格式应仅包含小时(个位数),不包含分钟。

"MM/dd/YYYY H a" // Still "a" wouldn't not work here

2.小​​时应采用12小时格式,即“h”而不是“H”,以便“a” 去工作。最后,删除“h”“a”之间的空格。

"MM/dd/YYYY ha" // Now you will get what you want!

You need to do some minor changes to the date format to make it work as you need.

1.Your date format should contain only the hour(single-digit) and no minutes.

"MM/dd/YYYY H a" // Still "a" wouldn't not work here

2.The hour should be in 12 hour format ie., "h" not "H" in order for "a" to work. Finally, remove the space between "h" and "a".

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