published
11 January 2025
by
Ray Morgan

Date and Time Formats

Section 9.3: Date and Time Formats in Different Writing Systems

Introduction

Date and time formats vary significantly across cultures, languages, and scripts. Proper localization of these formats ensures clarity and usability for users worldwide. Differences include calendar systems, date order (e.g., DD/MM/YYYY vs. MM/DD/YYYY), separators, time conventions (12-hour vs. 24-hour), and script-specific representations. This section explores these variations and provides implementation examples in PHP, Python, and JavaScript.


Examples and Challenges

  1. Examples of Format Variations

    • Date Order:
      • United States: MM/DD/YYYY (e.g., 01/11/2025).
      • Most of Europe: DD/MM/YYYY (e.g., 11/01/2025).
      • ISO 8601: YYYY-MM-DD (e.g., 2025-01-11).
    • Script-Specific Dates:
      • Arabic (e.g., ١١/٠١/٢٠٢٥).
      • Chinese: 年/月/日 (e.g., 2025年1月11日).
    • Time Formats:
      • 12-hour clock: 02:30 PM.
      • 24-hour clock: 14:30.
  2. Challenges

    • Calendar Systems: Support for non-Gregorian calendars, such as Hijri (Islamic), Buddhist, or Japanese.
    • Dynamic Content: Handling user-generated or server-generated timestamps that must adapt to the user’s locale.
    • Parsing and Validation: Parsing input in various localized formats while ensuring data consistency.
    • Time Zones: Representing and converting times accurately across different regions.

Implementation Solutions with Examples

  1. Locale-Aware Date and Time Formatting

    PHP:

    $date = new DateTime('2025-01-11 14:30:00', new DateTimeZone('UTC'));
    $formatter = new IntlDateFormatter('fr_FR', IntlDateFormatter::FULL, IntlDateFormatter::SHORT);
    $formatter->setTimeZone('Europe/Paris');
    echo $formatter->format($date); // Outputs: samedi 11 janvier 2025 à 15:30
    

    Python:

    from babel.dates import format_datetime
    from datetime import datetime
    dt = datetime(2025, 1, 11, 14, 30)
    formatted_date = format_datetime(dt, "full", locale="fr_FR")
    print(formatted_date)  # Outputs: samedi 11 janvier 2025 à 14:30
    

    JavaScript:

    const date = new Date('2025-01-11T14:30:00Z');
    const formatter = new Intl.DateTimeFormat('fr-FR', { dateStyle: 'full', timeStyle: 'short', timeZone: 'Europe/Paris' });
    console.log(formatter.format(date)); // Outputs: samedi 11 janvier 2025 à 15:30
    

  1. Script-Specific Dates

    PHP:

    $date = new DateTime('2025-01-11');
    $formatter = new IntlDateFormatter('ar-EG', IntlDateFormatter::FULL, IntlDateFormatter::NONE);
    echo $formatter->format($date); // Outputs: السبت، ١١ يناير ٢٠٢٥
    

    Python:

    from babel.dates import format_date
    dt = datetime(2025, 1, 11)
    formatted_date = format_date(dt, format="full", locale="ar_EG")
    print(formatted_date)  # Outputs: السبت، ١١ يناير ٢٠٢٥
    

    JavaScript:

    const date = new Date('2025-01-11');
    const formatter = new Intl.DateTimeFormat('ar-EG', { dateStyle: 'full' });
    console.log(formatter.format(date)); // Outputs: السبت، ١١ يناير ٢٠٢٥
    

  1. Non-Gregorian Calendars

    PHP:

    $date = new DateTime('2025-01-11');
    $formatter = new IntlDateFormatter('th-TH-u-ca-buddhist', IntlDateFormatter::FULL, IntlDateFormatter::NONE);
    echo $formatter->format($date); // Outputs: วันเสาร์ที่ 11 มกราคม พ.ศ. 2568
    

    Python:

    from babel.dates import format_date
    dt = datetime(2025, 1, 11)
    formatted_date = format_date(dt, format="full", locale="th_TH@calendar=buddhist")
    print(formatted_date)  # Outputs: วันเสาร์ที่ 11 มกราคม พ.ศ. 2568
    

    JavaScript:

    const date = new Date('2025-01-11');
    const formatter = new Intl.DateTimeFormat('th-TH-u-ca-buddhist', { dateStyle: 'full' });
    console.log(formatter.format(date)); // Outputs: วันเสาร์ที่ 11 มกราคม พ.ศ. 2568
    

  1. Time Zones

    PHP:

    $date = new DateTime('2025-01-11 14:30:00', new DateTimeZone('UTC'));
    $date->setTimezone(new DateTimeZone('Asia/Tokyo'));
    echo $date->format('Y-m-d H:i:s'); // Outputs: 2025-01-11 23:30:00
    

    Python:

    from datetime import datetime
    import pytz
    utc = pytz.utc
    tokyo = pytz.timezone('Asia/Tokyo')
    dt = utc.localize(datetime(2025, 1, 11, 14, 30))
    tokyo_time = dt.astimezone(tokyo)
    print(tokyo_time.strftime('%Y-%m-%d %H:%M:%S'))  # Outputs: 2025-01-11 23:30:00
    

    JavaScript:

    const date = new Date('2025-01-11T14:30:00Z');
    const formatter = new Intl.DateTimeFormat('en-US', { timeZone: 'Asia/Tokyo', timeStyle: 'medium', dateStyle: 'short' });
    console.log(formatter.format(date)); // Outputs: 1/12/2025, 11:30:00 AM
    

  1. User Preferences and Overrides
    • Allow users to choose their preferred date/time format and timezone settings.
    • Save these preferences in user profiles and apply dynamically.


Comments

Leave a question or comment: