如何在 iPhone 中打印 AM/PM 格式的时间?

发布于 2024-12-16 19:21:31 字数 58 浏览 0 评论 0原文

我想打印日期选择器中的时间,但包含上午/下午。我可以打印时间,但它永远不会打印上午/下午。我该怎么做?

I want to print the time from date picker but with the AM/PM. I can print the time, but it never prints the AM/PM. How can I do this?

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

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

发布评论

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

评论(6

走野 2024-12-23 19:21:31

使用下面的代码行

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"hh:mm a"];   
    NSString *str_date = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"str_date:%@",str_date);

Use below lines of code

    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormatter setDateFormat:@"hh:mm a"];   
    NSString *str_date = [dateFormatter stringFromDate:[NSDate date]];
    NSLog(@"str_date:%@",str_date);
画尸师 2024-12-23 19:21:31

您可以使用此文档获取更多信息 NSDateFormatter 类参考
一个例子可以是:

NSDate* date = [NSDate date];
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];

[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"date=%@",[dateFormatter stringFromDate:date]);

You could use this document for more information NSDateFormatter Class Reference
.An example could be:

NSDate* date = [NSDate date];
NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];

[formatter setTimeStyle:NSDateFormatterFullStyle];
NSLog(@"date=%@",[dateFormatter stringFromDate:date]);
心房敞 2024-12-23 19:21:31

您可以按如下方式设置 DateFormatter amSymbolpmSymbol

Xcode 8.3 • Swift 3.1

let formatter = DateFormatter()
formatter.dateFormat = "h:mm a 'on' MMMM dd, yyyy"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"

let dateString = formatter.string(from: Date())
print(dateString)   // "4:44 PM on June 23, 2016\n"

编辑:
Objective C 代码

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone systemTimeZone];
df.AMSymbol = @"AM";
df.PMSymbol = @"PM";
df.dateFormat = "h:mm a 'on' MMMM dd, yyyy"
NSString *stringDate = [df stringFromDate:dateFromString];

参考链接:
https://stackoverflow.com/a/31469237/2905967

You can set your DateFormatter amSymbol and pmSymbol as follow:

Xcode 8.3 • Swift 3.1

let formatter = DateFormatter()
formatter.dateFormat = "h:mm a 'on' MMMM dd, yyyy"
formatter.amSymbol = "AM"
formatter.pmSymbol = "PM"

let dateString = formatter.string(from: Date())
print(dateString)   // "4:44 PM on June 23, 2016\n"

Edit:
Objective C Code

NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.timeZone = [NSTimeZone systemTimeZone];
df.AMSymbol = @"AM";
df.PMSymbol = @"PM";
df.dateFormat = "h:mm a 'on' MMMM dd, yyyy"
NSString *stringDate = [df stringFromDate:dateFromString];

Ref link:
https://stackoverflow.com/a/31469237/2905967

缱绻入梦 2024-12-23 19:21:31

日期转字符串

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@“dd-MMM”];
cell.dateLabel.text = [formatter stringFromDate:item.pubDate]

日期格式符号
unicode 日期格式化页面中的表格应该足以让您构建自己想要的日期格式字符串...

Pattern Result (in a particular locale)
yyyy.MM.dd G ‘at’ HH:mm:ss zzz  1996.07.10 AD at 15:08:56 PDT
EEE, MMM d, ‘’yy    Wed, July 10, ‘96
h:mm a  12:08 PM
hh ‘o’‘clock’ a, zzzz   12 o’clock PM, Pacific Daylight Time
K:mm a, z   0:00 PM, PST
yyyyy.MMMM.dd GGG hh:mm aaa 01996.July.10 AD 12:08 PM
Hope this is useful to someone out there.

使用以下链接获取更多数据。

http://benscheirman.com/2010/ 06/dealing-with-dates-time-zones-in-objective-c/

这确实对我有帮助。这真的很容易。

Date to String

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@“dd-MMM”];
cell.dateLabel.text = [formatter stringFromDate:item.pubDate]

Date Format Symbols
The table from the unicode date formatting page should be enough for you to build your own desired date format string…

Pattern Result (in a particular locale)
yyyy.MM.dd G ‘at’ HH:mm:ss zzz  1996.07.10 AD at 15:08:56 PDT
EEE, MMM d, ‘’yy    Wed, July 10, ‘96
h:mm a  12:08 PM
hh ‘o’‘clock’ a, zzzz   12 o’clock PM, Pacific Daylight Time
K:mm a, z   0:00 PM, PST
yyyyy.MMMM.dd GGG hh:mm aaa 01996.July.10 AD 12:08 PM
Hope this is useful to someone out there.

Use the following link for more datails.

http://benscheirman.com/2010/06/dealing-with-dates-time-zones-in-objective-c/

this really helped me. It was really easy.

述情 2024-12-23 19:21:31

对于打印上午/下午,参数a/aa/aaa都将起作用。

NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myDateFormatter setDateFormat:@"hh:mm aaa"]; //a,aa or aa all will work.
[myDateFormatter setDateFormat:@"hh:mm aa"];
NSString *myDate = [myDateFormatter stringFromDate:[NSDate date]];
NSLog(@"myDate is :%@",myDate);

for printing AM/ PM the parameter a/aa/aaa all will work.

NSDateFormatter *myDateFormatter = [[[NSDateFormatter alloc] init] autorelease];
[myDateFormatter setDateFormat:@"hh:mm aaa"]; //a,aa or aa all will work.
[myDateFormatter setDateFormat:@"hh:mm aa"];
NSString *myDate = [myDateFormatter stringFromDate:[NSDate date]];
NSLog(@"myDate is :%@",myDate);
标点 2024-12-23 19:21:31

中使用:2019-12-19 05:42:04 AM

    var strDate1:String = "2019-12-19 05:42:04";
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let dateObj = dateFormatter.date(from: strDate1)
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss a"
    let strigDate =  dateFormatter.string(from: dateObj!)

您可以在 swift 5输出

You can use in swift 5

    var strDate1:String = "2019-12-19 05:42:04";
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
    let dateObj = dateFormatter.date(from: strDate1)
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss a"
    let strigDate =  dateFormatter.string(from: dateObj!)

Output: 2019-12-19 05:42:04 AM

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