“convertir 24 horas a 12 horas JavaScript” Código de respuesta

convertir 24 horas a 12 horas JavaScript

function tConvert (time) {
  // Check correct time format and split into components
  time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];

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

tConvert ('18:00:00');
Vivacious Vendace

Cómo convertir el tiempo a AM PM en JavaScript

var suffix = hour >= 12 ? "PM":"AM";
var hours = ((hour + 11) % 12 + 1) + suffix
Funny Finch

Javascript de 12 horas a 24 horas


var time = $("#starttime").val();
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);

Cooperative Cicada

Javascript de 12 horas a 24 horas

const time = '5:00AM';

function convertTo24HrsFormat(time) {
   const slicedTime = time.split(/(PM|AM)/gm)[0];

   let [hours, minutes] = slicedTime.split(':');

   if (hours === '12') {
      hours = '00';
   }

   let updateHourAndMin;

   function addition(hoursOrMin) {
      updateHourAndMin =
         hoursOrMin.length < 2
            ? (hoursOrMin = `${0}${hoursOrMin}`)
            : hoursOrMin;

      return updateHourAndMin;
   }

   if (time.endsWith('PM')) {
      hours = parseInt(hours, 10) + 12;
   }

   return `${addition(hours)}:${addition(minutes)}`;
}

console.log(`Converted time: ${convertTo24HrsFormat(time)}`);
Mehedi Islam Ripon

convertir 24 horas a 12 horas JavaScript

convert 24 hore into 12 hr  javascript code
Mysterious Mamba

convertir 24 horas a 12 horas JavaScript

javascript code 24 hr into 12 hre format
Mysterious Mamba

Respuestas similares a “convertir 24 horas a 12 horas JavaScript”

Preguntas similares a “convertir 24 horas a 12 horas JavaScript”

Más respuestas relacionadas con “convertir 24 horas a 12 horas JavaScript” en JavaScript

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código