0

I have an array of timings.

let data = ["09:00:00", "09:10:00", "09:20:00", "09:30:00", "09:40:00", "09:50:00", "10:00:00", "10:10:00", "10:20:00", "10:30:00", "10:40:00", "10:50:00", "11:00:00", "11:10:00", "11:20:00", "11:30:00", "11:40:00", "11:50:00", "12:00:00", "12:10:00", "12:20:00", "12:30:00", "12:40:00", "12:50:00", "13:00:00", "13:10:00", "13:20:00", "13:30:00", "13:40:00", "13:50:00", "14:00:00", "14:10:00", "14:20:00", "14:30:00", "14:40:00", "14:50:00", "15:00:00", "15:10:00", "15:20:00", "15:30:00", "15:40:00", "15:50:00"]

How to check each time is am or pm using javascript?

  • 1
    Does this answer your question? [Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone](https://stackoverflow.com/questions/13898423/javascript-convert-24-hour-time-of-day-string-to-12-hour-time-with-am-pm-and-no) – Shahnawaz Hossan Dec 21 '20 at 16:55
  • 5
    [How do you display JavaScript datetime in 12 hour AM/PM format?](https://stackoverflow.com/questions/8888491/how-do-you-display-javascript-datetime-in-12-hour-am-pm-format) – Alex Dec 21 '20 at 16:56
  • 1
    Just use [*parseInt*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt): `if (parseInt(data[i]] > 11) /* it's pm */`. – RobG Dec 21 '20 at 22:38

2 Answers2

2

Split the string by :. Convert the first part to a number then compare with 12:

let data = ["09:00:00", "09:10:00", "09:20:00", "09:30:00", "09:40:00", "09:50:00", "10:00:00", "10:10:00", "10:20:00", "10:30:00", "10:40:00", "10:50:00", "11:00:00", "11:10:00", "11:20:00", "11:30:00", "11:40:00", "11:50:00", "12:00:00", "12:10:00", "12:20:00", "12:30:00", "12:40:00", "12:50:00", "13:00:00", "13:10:00", "13:20:00", "13:30:00", "13:40:00", "13:50:00", "14:00:00", "14:10:00", "14:20:00", "14:30:00", "14:40:00", "14:50:00", "15:00:00", "15:10:00", "15:20:00", "15:30:00", "15:40:00", "15:50:00"];

function checkAMorPM(time) {
  var hrs = Number(time.split(":")[0]);
  return (hrs >= 12 ? "PM" : "AM");
}

for(var i = 0; i < data.length; i++) {
  console.log(`${data[i]}: ${checkAMorPM(data[i])}`);
}

Or if you prefer, you can do it in a single line:

const checkAMorPM = time => +time.split(':')[0] < 12 ? "AM" : "PM";
Wais Kamal
  • 5,858
  • 2
  • 17
  • 36
0

Don't worry about splitting the value - simply convert it to a number using parseInt() - which will only convert the value (up to the first non-numerical value) into a number that can then be checked against 12.

So because he character ":" is invalid for an integer - only the first two characters will be parsed - meaning that "09:00:00" will be treated as if its "09" and parsed to the integer 9 and when compared against 12 - will console out "AM".

let data = ["09:00:00", "09:10:00", "09:20:00", "09:30:00", "09:40:00", "09:50:00", "10:00:00", "10:10:00", "10:20:00", "10:30:00", "10:40:00", "10:50:00", "11:00:00", "11:10:00", "11:20:00", "11:30:00", "11:40:00", "11:50:00", "12:00:00", "12:10:00", "12:20:00", "12:30:00", "12:40:00", "12:50:00", "13:00:00", "13:10:00", "13:20:00", "13:30:00", "13:40:00", "13:50:00", "14:00:00", "14:10:00", "14:20:00", "14:30:00", "14:40:00", "14:50:00", "15:00:00", "15:10:00", "15:20:00", "15:30:00", "15:40:00", "15:50:00"];


data.forEach(function(item){
 parseInt(item, 10) < 12 
   ? console.log('AM')
   : console.log('PM')
})
gavgrif
  • 15,194
  • 2
  • 25
  • 27