Using the time-slots-generator package, I am able to display the time from 0:15 to 24:00. However, the issue is that this package does not have built-in functionality to display AM/PM, so I had to create a manual solution for it.
To achieve this, I modified some code snippets found on StackOverflow and came up with the following implementation:
let hours = 23;
let minutes = 32;
let ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12;
minutes = minutes < 10 ? '0'+minutes : minutes;
const strTime = hours + ':' + minutes + ' ' + ampm;
console.log(strTime)
If you change the values of hours to 17 and minutes to 25, the output will be 17:25 pm. The function logic is straightforward - it determines whether to display AM or PM based on the input value.
Now, my goal is to integrate this logic with my time generator. Currently, I am using a loop to display time from 0:15 to 24:00 as shown below:
data() {
return {
timeSlots: (ts.getTimeSlots([], true, "quarter")),
}
},
formatAMPM() {
let val = Object.values(this.timeSlots);
for (let prop in val) {
console.log(val[prop]);
}
}
The current result of this loop can be seen here:https://i.sstatic.net/X3xdG.jpg
I now need to update the loop to display times in either AM or PM format. If you have a simpler solution or advice on how to implement this, I would greatly appreciate it. You can also view an example in codesandbox here.