Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

203
Views
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>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

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>

about 4 years ago · Juan Pablo Isaza Report

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>

about 4 years ago · Juan Pablo Isaza Report

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];
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!