• Empleos
  • Sobre nosotros
  • profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
  • empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Pruebas Online
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

130
Vistas
How to convert military to standard Time in javascript using the below example?

I want the output to say 11pm for example, instead of 2300. I am not sure how to convert it. Everything else is working out how I want it to but I don't want the output in military time

 <script>

    var d = new Date();
    var n = d.getDay();
    var now = d.getHours() + "." + d.getMinutes();
    var weekdays = [
        ["Sunday", 13.00, 1700],
        ["Monday", 9.00, 2200],
        ["Tuesday", 9.00, 2200],
        ["Wednesday", 9.00, 2200],
        ["Thursday",   9.00, 2200],
        ["Friday", 9.00, 19.00],
        ["Saturday", 9.00, 17.00]
    ];
    var day = weekdays[n];

;
    if (now > day[1] && now < day[2] || now > day[3] && now < day[4]) {
        console.log("We are open today from "+ day[1]);
         document.getElementById('example').innerHTML = "We are open today from "+day[1]+"AM"+" to "+day[2];
    }
     else {
        console.log("We are currently closed. We will open at");
        document.getElementById('example').innerHTML = "We are currently closed. We will open at"+ day[1];
    }

</script>
almost 3 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You might want to display opening and closing time as Date objects, so comparing them with current date would be easier. Also you should consider using a partial to create dates because we know that the year, month and day of the month would be constant among all schedule records. Also it's recommended to use JS provided interfaces to manipulate dates when it's possible. Here we better use toLocaleString to get a time format as we wish. So the below snippet of code might be useful:

const currentDate = new Date();

const createShceduleDate = (hours, minutes) =>
  new Date(
    currentDate.getFullYear(),
    currentDate.getMonth(),
    currentDate.getDate(),
    hours,
    minutes
  );

const get12HourDate = (date) =>
  date.toLocaleString("en-US", { hour: "numeric", hour12: true });

const schedule = [
  ["Sunday", createShceduleDate(13, 0), createShceduleDate(17, 0)],
  ["Monday", createShceduleDate(9, 0), createShceduleDate(22, 0)],
  ["Tuesday", createShceduleDate(9, 0), createShceduleDate(22, 0)],
  ["Wednesday", createShceduleDate(9, 0), createShceduleDate(22, 0)],
  ["Thursday", createShceduleDate(9, 0), createShceduleDate(22, 0)],
  ["Friday", createShceduleDate(9, 0), createShceduleDate(22, 0)],
  ["Saturday", createShceduleDate(9, 0), createShceduleDate(22, 0)],
];

const matchingDate = schedule[currentDate.getDay()];

if (matchingDate[1] <= currentDate && matchingDate[2] >= currentDate) {
  console.log(
    `We are currently open. (${matchingDate[0]}, from ${get12HourDate(
      matchingDate[1]
    )} to ${get12HourDate(matchingDate[2])})`
  );
} else {
  document.getElementById(
    "example"
  ).innerHTML = `We are currently closed. We will open at ${get12HourDate(
    schedule[(currentDate.getDay() + 1) % 6][1]
  )}`;
}
<p id="example"></p>

almost 3 years ago · Juan Pablo Isaza Denunciar

0

I would write a helper function to convert military hours to am/pm.

const now = new Date();
const hoursOfOperation = [
  {open: 13, close: 17},
  {open: 9, close: 22},
  {open: 9, close: 22},
  {open: 9, close: 22},
  {open: 9, close: 22},
  {open: 9, close: 19},
  {open: 9, close: 17}
];

function hourToString(hour)
{
  if (hour === 0) return "Midnight";
  else if (hour === 12) return "Noon";
  else if (hour > 12) return `${hour-12} PM`;
  else return `${hour} AM`;
  
  // Shorter version without Midnight and Noon
  // return ((hour + 11) % 12 + 1) + (hour >= 12 ? " PM":" AM");
}

const today = hoursOfOperation[now.getDay()];
if (now.getHours() >= today.open && now.getHours() < today.close) {
  document.getElementById('example').innerHTML = `We are open today from ${hourToString(today.open)} to ${hourToString(today.close)}`;
}
else {
  const nextDay = hoursOfOperation[(now.getDay() + 1) % 6];
  document.getElementById('example').innerHTML = `We are currently closed. We will open tomorrow at ${hourToString(nextDay.open)}`;
}
<div id='example'></div>

almost 3 years ago · Juan Pablo Isaza Denunciar

0

I will use a function to convert the military time.

function convertTime24to12(militarytime) {
    let time = militarytime/100
    let hours = parseInt(time, 10)-12
    return hours
}

var d = new Date();
var n = d.getDay();
var now = d.getHours() + "." + d.getMinutes();
var weekdays = [
    ["Sunday", 13.00, 1700],
    ["Monday", 9.00, 2200],
    ["Tuesday", 9.00, 2200],
    ["Wednesday", 9.00, 2200],
    ["Thursday",   9.00, 2200],
    ["Friday", 9.00, 1900],
    ["Saturday", 9.00, 1700]
];

var day = weekdays[n];

    if (now > day[1] && now < day[2] || now > day[3] && now < day[4]) {
        // console.log("We are open today from "+ day[1]);
        console.log("We are open today from "+day[1]+"AM"+" to "+convertTime24to12(day[2])+"PM");
        //  document.getElementById('example').innerHTML = "We are open today from "+day[1]+"AM"+" to "+convertTime24to12(day[2])+"PM";
    }
     else {
        console.log("We are currently closed. We will open at " + day[1]);
        // document.getElementById('example').innerHTML = "We are currently closed. We will open at"+ day[1];
    }
almost 3 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda