gpt4 book ai didi

iphone - 如何获取日期和时间 (MM/dd/yyyy hh :MM a) format?

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:32 25 4
gpt4 key购买 nike

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

10/19/2011 3pm

目前我正在写这段代码,

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

但以这种格式获取日期:

10/19/2011 15.00

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

最佳答案

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

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!

关于iphone - 如何获取日期和时间 (MM/dd/yyyy hh :MM a) format?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7819888/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com