Thought leadership from the most innovative tech companies, all in one place.

Convert Time From AM/PM to 24-Hour Format Using JavaScript

A tutorial on converting time from AM/PM to 24-hour format using JavaScript.

image

Hello, how are you all friends, I hope you are always healthy and successful.

This time I just want to share the JavaScript code on how to convert am or pm time to 24 hours, hopefully, this tutorial is useful for all of you.

Okay, let’s just go to the tutorial this time.

var hrs = Number(timestart.match(/^(\d+)/)[1]);
 var mnts = Number(timestart.match(/:(\d+)/)[1]);
 var format = timestart.match(/\s(.*)$/)[1];
     if (format == "PM" && hrs < 12) hrs = hrs + 12;
     if (format == "AM" && hrs == 12) hrs = hrs - 12;
        var hours = hrs.toString();
        var minutes = mnts.toString();
            if (hrs < 10) hours = "0" + hours;
            if (mnts < 10) minutes = "0" + minutes;
            var timeend = hours + ":" + minutes + ":00";
            console.log(timeend); //h:i:s

If we look at the code above, it will change the am/pm format to 24 hours.

var hrs = Number(timestart.match(/^(\d+)/)[1]);
 var mnts = Number(timestart.match(/:(\d+)/)[1]);

We look at the code above, we will create two variables namely hours and minutes, and next, we will create conditions.

if (format == "PM" && hrs < 12) hrs = hrs + 12;
     if (format == "AM" && hrs == 12) hrs = hrs - 12;

The code above is the code to create the condition at the time it is created, and next, we will separate the hours and minutes.

if (hrs < 10) hours = "0" + hours;
  if (mnts < 10) minutes = "0" + minutes;
  var times = hours + ":" + minutes + ":00";

And we will get the time in 24-hour format.

Convert 24-hour time to am/pm

We will change back the 24-hour time format to am/pm. This is the code to change back in the previous format.

d = new Date(times);
var timerfix = d.getHours() + ':' + d.getMinutes();

  timerfix = timerfix.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [timerfix];

  if (timerfix.length > 1) { // If time format correct
      timerfix = timerfix.slice (1);  // Remove full string match value
      timerfix[5] = +timerfix[0] < 12 ? ' AM' : ' PM'; // Set AM/PM
      timerfix[0] = +timerfix[0] % 12 || 12; // Adjust hours
  }

  console.log(timerfix.join(''));

We’ll get back the hours and minutes, on that code.

var timerfix = d.getHours() + ':' + d.getMinutes();

Next, we will change the code to type string.

timerfix = timerfix.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [timerfix];

After getting time in string format then we solve in condition.

if (timerfix.length > 1) { // If time format correct
      timerfix = timerfix.slice (1);  // Remove full string match value
      timerfix[5] = +timerfix[0] < 12 ? ' AM' : ' PM'; // Set AM/PM
      timerfix[0] = +timerfix[0] % 12 || 12; // Adjust hours
  }

Then we will get back the time in am/pm format.

Hopefully, this tutorial is useful for all of you. This is a simple code, sometimes we forget how to use it.

Thanks.

Source: https://temanngoding.com/tutorial-javascript-convert-waktu-am-pm-to-24-jam/




Continue Learning