如何在 Objective-C 中以 12 小时格式显示时间?

发布于 2024-12-28 09:51:13 字数 452 浏览 3 评论 0原文

我正在开发 iPhone 应用程序,我需要在标签中显示时间。所以我写了以下内容:

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm a"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
timeLabel.text = formattedDateString;**

它在标签中显示时间,但以 24 小时格式。

  • 示例:下午 15 点而不是下午 3 点

我想以 12 小时格式显示时间,即下午 3 点

我应该做什么?

提前致谢。

I was working on an iPhone application, and I need to display the time in a label. So I wrote the follow:

NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"HH:mm a"];
NSString *formattedDateString = [dateFormatter stringFromDate:date];
timeLabel.text = formattedDateString;**

It displays time in label but in 24H format.

  • Example: 15.00 PM instead of 3.00 PM

I want to display time in 12h format, i.e. 3.00 PM

What should I do?

Thanks in advance.

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

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

发布评论

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

评论(5

唔猫 2025-01-04 09:51:13

[dateFormatter setDateFormat:@"hh:mm a"];

注意小写的 h。

[dateFormatter setDateFormat:@"hh:mm a"];

Note the lower case h's.

撩起发的微风 2025-01-04 09:51:13
    Date Formats a: AM/PM
    A: 0~86399999 (Millisecond of Day)

    c/cc: 1~7 (Day of Week)
    ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
    cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

    d: 1~31 (0 padded Day of Month)
    D: 1~366 (0 padded Day of Year)

    e: 1~7 (0 padded Day of Week)
    E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat
    EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

    F: 1~5 (0 padded Week of Month, first day of week = Monday)

    g: Julian Day Number (number of days since 4713 BC January 1)
    G~GGG: BC/AD (Era Designator Abbreviated)
    GGGG: Before Christ/Anno Domini

    h: 1~12 (0 padded Hour (12hr))
    H: 0~23 (0 padded Hour (24hr))

    k: 1~24 (0 padded Hour (24hr)
    K: 0~11 (0 padded Hour (12hr))

    L/LL: 1~12 (0 padded Month)
    LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    LLLL: January/February/March/April/May/June/July/August/September/October/November/December

    m: 0~59 (0 padded Minute)
    M/MM: 1~12 (0 padded Month)
    MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    MMMM: January/February/March/April/May/June/July/August/September/October/November/December

    q/qq: 1~4 (0 padded Quarter)
    qqq: Q1/Q2/Q3/Q4
    qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter
    Q/QQ: 1~4 (0 padded Quarter)
    QQQ: Q1/Q2/Q3/Q4
    QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter

    s: 0~59 (0 padded Second)
    S: (rounded Sub-Second)

    u: (0 padded Year)

    v~vvv: (General GMT Timezone Abbreviation)
    vvvv: (General GMT Timezone Name)

    w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
    W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)

    y/yyyy: (Full Year)
    yy/yyy: (2 Digits Year)
    Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
    YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)

    z~zzz: (Specific GMT Timezone Abbreviation)
    zzzz: (Specific GMT Timezone Name)
    Z: +0000 (RFC 822 Timezone)

How to Use:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"%@", [dateFormatter dateFromString:@"12/12/2012 12:12:12 AM"]);
    Date Formats a: AM/PM
    A: 0~86399999 (Millisecond of Day)

    c/cc: 1~7 (Day of Week)
    ccc: Sun/Mon/Tue/Wed/Thu/Fri/Sat
    cccc: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

    d: 1~31 (0 padded Day of Month)
    D: 1~366 (0 padded Day of Year)

    e: 1~7 (0 padded Day of Week)
    E~EEE: Sun/Mon/Tue/Wed/Thu/Fri/Sat
    EEEE: Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday

    F: 1~5 (0 padded Week of Month, first day of week = Monday)

    g: Julian Day Number (number of days since 4713 BC January 1)
    G~GGG: BC/AD (Era Designator Abbreviated)
    GGGG: Before Christ/Anno Domini

    h: 1~12 (0 padded Hour (12hr))
    H: 0~23 (0 padded Hour (24hr))

    k: 1~24 (0 padded Hour (24hr)
    K: 0~11 (0 padded Hour (12hr))

    L/LL: 1~12 (0 padded Month)
    LLL: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    LLLL: January/February/March/April/May/June/July/August/September/October/November/December

    m: 0~59 (0 padded Minute)
    M/MM: 1~12 (0 padded Month)
    MMM: Jan/Feb/Mar/Apr/May/Jun/Jul/Aug/Sep/Oct/Nov/Dec
    MMMM: January/February/March/April/May/June/July/August/September/October/November/December

    q/qq: 1~4 (0 padded Quarter)
    qqq: Q1/Q2/Q3/Q4
    qqqq: 1st quarter/2nd quarter/3rd quarter/4th quarter
    Q/QQ: 1~4 (0 padded Quarter)
    QQQ: Q1/Q2/Q3/Q4
    QQQQ: 1st quarter/2nd quarter/3rd quarter/4th quarter

    s: 0~59 (0 padded Second)
    S: (rounded Sub-Second)

    u: (0 padded Year)

    v~vvv: (General GMT Timezone Abbreviation)
    vvvv: (General GMT Timezone Name)

    w: 1~53 (0 padded Week of Year, 1st day of week = Sunday, NB: 1st week of year starts from the last Sunday of last year)
    W: 1~5 (0 padded Week of Month, 1st day of week = Sunday)

    y/yyyy: (Full Year)
    yy/yyy: (2 Digits Year)
    Y/YYYY: (Full Year, starting from the Sunday of the 1st week of year)
    YY/YYY: (2 Digits Year, starting from the Sunday of the 1st week of year)

    z~zzz: (Specific GMT Timezone Abbreviation)
    zzzz: (Specific GMT Timezone Name)
    Z: +0000 (RFC 822 Timezone)

How to Use:

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"dd/MM/yyyy hh:mm:ss a"];
NSLog(@"%@", [dateFormatter dateFromString:@"12/12/2012 12:12:12 AM"]);
似狗非友 2025-01-04 09:51:13

看看 UNICODE LOCALE DATA MARKUP LANGUAGE

猜你得用 KK而不是呵呵...

Just take a look UNICODE LOCALE DATA MARKUP LANGUAGE

Guess you have to use KK instead of HH ...

久随 2025-01-04 09:51:13

阅读文档是一个很好的做法。

HH- 在 2、3、4 点时仅显示 0。这不是 0-12 AM/PM 格式。

有关更多详细信息,请参阅“日期格式化程序”。

根据您的情况:

附录 F:日期格式模式

h:mm a -> 3:00 PM

Reading the documentation is a good practice.

HH- is just showing the 0 when its 2,3,4 oclock. It's NOT 0-12 AM/PM format.

See “Date Formatters” there for more details.

in your case:

Appendix F: Date Format Patterns

h:mm a -> 3:00 PM
谈场末日恋爱 2025-01-04 09:51:13

斯威夫特3.0

dateFormatter.dateFormat = "hh:mm a"

Swift 3.0

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