Методы работы с форматами дат в языках программирования: примеры на JavaScript, Python, Java, PHP и Ruby

Вот несколько методов работы с форматами дат в различных языках программирования. Я приведу примеры кода для каждого метода:

  1. JavaScript:

    // Get current date in ISO 8601 format
    const currentDate = new Date().toISOString();
    console.log(currentDate);
    // Format a date using a library like moment.js
    const formattedDate = moment(currentDate).format('YYYY-MM-DD');
    console.log(formattedDate);
  2. Python:

    from datetime import datetime
    # Get current date in ISO 8601 format
    current_date = datetime.now().isoformat()
    print(current_date)
    # Format a date using strftime
    formatted_date = datetime.now().strftime('%Y-%m-%d')
    print(formatted_date)
  3. Java:

    import java.time.LocalDateTime;
    import java.time.format.DateTimeFormatter;
    // Get current date in ISO 8601 format
    LocalDateTime currentDateTime = LocalDateTime.now();
    String currentDate = currentDateTime.toString();
    System.out.println(currentDate);
    // Format a date using DateTimeFormatter
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    String formattedDate = currentDateTime.format(formatter);
    System.out.println(formattedDate);
  4. PHP:

    // Get current date in ISO 8601 format
    $currentDate = date('c');
    echo $currentDate;
    // Format a date using date and strtotime functions
    $formattedDate = date('Y-m-d', strtotime($currentDate));
    echo $formattedDate;
  5. Рубин:

    require 'date'
    # Get current date in ISO 8601 format
    currentDate = DateTime.now.iso8601
    puts currentDate
    # Format a date using strftime
    formattedDate = DateTime.now.strftime('%Y-%m-%d')
    puts formattedDate