
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript program to convert 24 hours format to 12 hours
Following is the code to convert 24 hours format to 12 hours in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } </style> </head> <body> <h1>Convert 24 hours format to 12 hours</h1> <label>Hours </label><input class="hour" type="number"></br> <label>Minutes </label><input class="minute" type="number"><br/> <div class="result"></div><br /> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to convert the above 24 hour format to 12 hour format</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); let hrEle,minEle; BtnEle.addEventListener("click", () => { hrEle = document.querySelector('.hour').value; minEle = document.querySelector('.minute').value; if(hrEle>=0 && hrEle<=24 && minEle >=0 && minEle <= 60){ let AMorPM='AM'; if(hrEle>12)AMorPM='PM'; hrEle = (hrEle % 12); resEle.innerHTML = 'TIME = '+hrEle+' : '+minEle+' '+AMorPM; } }); </script> </body> </html>
Output
Entering a valid time in the 24-hour format and click on the ‘CLICK HERE’ button −
- Related Questions & Answers
- C# program to convert time from 12 hour to 24 hour format
- Python program to convert time from 12 hour to 24 hour format
- C++ program to convert time from 12 hour to 24 hour format
- Converting 12 hour format time to 24 hour format in JavaScript
- MySQL query to select records approaching in next 12 hours?
- Convert time from 24 hour clock to 12 hour clock format in C++
- Python program to convert seconds into hours, minutes and seconds
- How to add 2 hours to a JavaScript Date object?
- Get the time difference and convert it to hours in MySQL?
- How to convert a MySQL TIME value to days and hours form?
- How to find the files in Linux that have been changed in the last 24 hours?
- How to convert 12-hour time scale to 24-hour time in R?
- How to add hours and minutes to a date with JavaScript?
- How to convert string to 24-hour datetime format in MySQL?
- C# Program to determine the difference in hours between two dates
Advertisements