Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone

Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone

To convert a 24-hour time-of-day string to a 12-hour time format with AM/PM in JavaScript, you can use the Date object to parse and format the time. Here's a function that accomplishes this:

function convertTimeTo12HourFormat(time24) { // Split the time string into hours and minutes var [hours, minutes] = time24.split(':'); // Create a Date object (we don't need the date part) var date = new Date(); date.setHours(parseInt(hours, 10)); date.setMinutes(parseInt(minutes, 10)); // Get hours and minutes in 12-hour format var hours12 = date.getHours() % 12; hours12 = hours12 || 12; // Handle midnight (0 hours) // Get AM/PM var period = (date.getHours() < 12) ? 'AM' : 'PM'; // Format the time as HH:MM AM/PM var time12 = hours12 + ':' + minutes + ' ' + period; return time12; } // Example usage: var time24 = '14:30'; var time12 = convertTimeTo12HourFormat(time24); console.log(time12); // Output: 2:30 PM 

Explanation:

  1. Splitting Time: Split the input time24 string into hours and minutes using split(':').

  2. Creating Date Object: Create a Date object using the parsed hours and minutes. This sets up the time within the context of the current date, which helps in handling the conversion to 12-hour format correctly.

  3. Getting 12-hour Format:

    • Calculate hours12 by using modulo operation to ensure it wraps around correctly for 12-hour format.
    • Adjust hours12 to 12 when hours12 is 0 (midnight).
  4. Determining AM/PM: Check if the hours are less than 12 to decide whether it's AM or PM.

  5. Formatting: Concatenate hours12, minutes, and AM/PM (period) into a string format "HH:MM AM/PM".

  6. Example Usage: Use the convertTimeTo12HourFormat function with an example time24 string ('14:30'), and log the converted time ('2:30 PM').

This function assumes the input time24 is a valid 24-hour format string in the format 'HH:MM'. Adjustments may be needed for error handling or edge cases depending on specific use cases (e.g., handling invalid input strings).

Examples

  1. Convert 24-hour time string to 12-hour time with AM/PM in JavaScript

    • Description: Convert a time string from 24-hour format (e.g., "15:30") to 12-hour format with AM/PM (e.g., "3:30 PM").
    • Code:
      function convertTimeTo12HourFormat(time24) { const [hour, minute] = time24.split(':'); let period = 'AM'; let hour12 = parseInt(hour); if (hour12 >= 12) { period = 'PM'; if (hour12 > 12) { hour12 -= 12; } } return `${hour12}:${minute} ${period}`; } // Example usage: console.log(convertTimeTo12HourFormat('15:30')); // Outputs: 3:30 PM 
  2. JavaScript function to convert military time to standard time

    • Description: Implement a JavaScript function to convert military (24-hour) time format to standard 12-hour time format with AM/PM.
    • Code:
      function militaryToStandardTime(militaryTime) { const [hours, minutes] = militaryTime.split(':'); const period = hours >= 12 ? 'PM' : 'AM'; const standardHours = hours % 12 || 12; return `${standardHours}:${minutes} ${period}`; } // Example usage: console.log(militaryToStandardTime('18:45')); // Outputs: 6:45 PM 
  3. Convert time from 24-hour format to 12-hour format in JavaScript

    • Description: Write a JavaScript function to convert a 24-hour time string to a 12-hour time format string including AM/PM indication.
    • Code:
      function convertTime12to24(time12) { const [time, period] = time12.split(' '); let [hour, minute] = time.split(':'); hour = parseInt(hour); if (period === 'PM' && hour < 12) hour += 12; if (period === 'AM' && hour === 12) hour = 0; return `${hour}:${minute}`; } // Example usage: console.log(convertTime12to24('3:30 PM')); // Outputs: 15:30 
  4. JavaScript convert 24-hour time to 12-hour format example

    • Description: Example JavaScript code snippet demonstrating conversion of a 24-hour time string to a 12-hour format with AM/PM.
    • Code:
      function convertTo12HourFormat(time24) { const [hour, minute] = time24.split(':'); const period = hour >= 12 ? 'PM' : 'AM'; const hour12 = (hour % 12) || 12; return `${hour12}:${minute} ${period}`; } // Example usage: console.log(convertTo12HourFormat('14:45')); // Outputs: 2:45 PM 
  5. JavaScript function to convert military time to standard time with AM/PM

    • Description: Implement a JavaScript function that converts military time (24-hour format) to standard time (12-hour format) including AM/PM designation.
    • Code:
      function militaryToStandard(militaryTime) { let [hours, minutes] = militaryTime.split(':'); const period = hours >= 12 ? 'PM' : 'AM'; hours %= 12; hours = hours ? hours : 12; // Handle midnight case (00:xx) return `${hours}:${minutes} ${period}`; } // Example usage: console.log(militaryToStandard('23:15')); // Outputs: 11:15 PM 
  6. JavaScript convert 24-hour time to 12-hour with AM/PM format

    • Description: JavaScript function to convert a 24-hour time string into a 12-hour format string with AM/PM indicator.
    • Code:
      function convert24To12(time24) { let [hour, minute] = time24.split(':'); const period = hour >= 12 ? 'PM' : 'AM'; hour = hour % 12 || 12; return `${hour}:${minute} ${period}`; } // Example usage: console.log(convert24To12('09:30')); // Outputs: 9:30 AM 
  7. JavaScript convert military time to standard time format

    • Description: JavaScript function to convert a military time string (24-hour format) to a standard 12-hour time format with AM/PM.
    • Code:
      function convertMilitaryToStandard(militaryTime) { const [hours, minutes] = militaryTime.split(':'); const period = hours >= 12 ? 'PM' : 'AM'; const standardHours = hours % 12 || 12; return `${standardHours}:${minutes} ${period}`; } // Example usage: console.log(convertMilitaryToStandard('16:00')); // Outputs: 4:00 PM 
  8. Convert 24-hour time string to 12-hour time with AM/PM using JavaScript

    • Description: A JavaScript function to convert a 24-hour time string to a 12-hour time format with AM/PM.
    • Code:
      function convertTimeTo12Hour(time24) { const [hour, minute] = time24.split(':'); const period = hour >= 12 ? 'PM' : 'AM'; const hour12 = hour % 12 || 12; return `${hour12}:${minute} ${period}`; } // Example usage: console.log(convertTimeTo12Hour('17:20')); // Outputs: 5:20 PM 
  9. JavaScript function to change 24-hour time format to 12-hour time

    • Description: JavaScript function to convert a 24-hour time string to a 12-hour time format with AM/PM appended.
    • Code:
      function convert24To12Hour(time24) { const [hour, minute] = time24.split(':'); const period = hour >= 12 ? 'PM' : 'AM'; const hour12 = hour % 12 || 12; return `${hour12}:${minute} ${period}`; } // Example usage: console.log(convert24To12Hour('13:45')); // Outputs: 1:45 PM 
  10. JavaScript convert military time to standard time with AM/PM example

    • Description: Example of JavaScript code to convert military (24-hour) time to standard 12-hour time with AM/PM notation.
    • Code:
      function militaryToStandardTime(militaryTime) { const [hours, minutes] = militaryTime.split(':'); const period = hours >= 12 ? 'PM' : 'AM'; const standardHours = hours % 12 || 12; return `${standardHours}:${minutes} ${period}`; } // Example usage: console.log(militaryToStandardTime('14:00')); // Outputs: 2:00 PM 

More Tags

scala hint clip-path swiftui azure-logic-apps ibaction laravel-excel blazor-webassembly mach distribution

More Programming Questions

More Various Measurements Units Calculators

More Everyday Utility Calculators

More Physical chemistry Calculators

More Electrochemistry Calculators