如何获得日期和时间(MM/dd/ yyyyyyyyyhh:MM a)格式?

时间:2022-03-16 16:55:25

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

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

10/19/2011 3pm

10/19/2011 3点

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

10/19/2011 15.00

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

那么,我怎么才能在这个10/19/2011 3pm格式中得到它呢?

1 个解决方案

#1


11  

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.

1。您的日期格式应该只包含小时(个位数)和分钟。

"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".

2。时间应该是12小时的格式。,“h”而不是“h”,以便“a”工作。最后,去掉“h”和“a”之间的空格。

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

#1


11  

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.

1。您的日期格式应该只包含小时(个位数)和分钟。

"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".

2。时间应该是12小时的格式。,“h”而不是“h”,以便“a”工作。最后,去掉“h”和“a”之间的空格。

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