How to Add Pm or Am Based on Hour from Input

How to add PM or AM based on hour from input?

You need something like this. I have allowed 24 hours in max value input, but then in javascript I substract 12 hours if it's in PM. As you can see, it is possible check the value of an input without parsing it to int. Finally, you concatenate PM or AM to your result.

function myFunction() {  var value = document.getElementById('number').value;
if (value > 12) { value = value - 12; document.getElementById('result').innerHTML = value + "PM"; } else { document.getElementById('result').innerHTML = value + "AM"; }}
<input type='Number' id='number' max="24" min="0" value="0">
<button onclick="myFunction()">Try it</button><br><p id="result"></p>

Get AM/PM from HTML time input

As far as i know, We cannot get the value from the input directly in the meridian format of 'AM/PM'. Instead, we can write our own logic of converting the value in the AM/PM format and store/display it.

Below is the implementation of it.

var inputEle = document.getElementById('timeInput');

function onTimeChange() { var timeSplit = inputEle.value.split(':'), hours, minutes, meridian; hours = timeSplit[0]; minutes = timeSplit[1]; if (hours > 12) { meridian = 'PM'; hours -= 12; } else if (hours < 12) { meridian = 'AM'; if (hours == 0) { hours = 12; } } else { meridian = 'PM'; } alert(hours + ':' + minutes + ' ' + meridian);}
<input type="time" onchange="onTimeChange()" id="timeInput" />

How to get "AM/PM" in data read from a form input (type=time)?

if

the output of this is only the numbers

then it's probably a timestamp, and you'll have for example to use date() to turn it into readable date format.

Example giving March 10, 2001, 5:16 pm :
(assuming $time is a timestamp long number)

<?php echo date("F j, Y, g:i a", $time); ?>

I want AM and PM along with time in <input type="time">

You can use moment.js library to get AM/PM with input time.

moment(start_time,'hh:mm').format('hh:mm a')

Convert AM/PM to 24 hours clock in Laravel input field

You'll need to change your time field to the following

{{Form::time('time', \Carbon\Carbon::now()->timezone('Europe/Brussels')->format('H:i:s'),['class' => 'form-control'])}}

To explain the formatting;

H - 24 Hour with trailing zeros

i - Minutes with trailing zeros

s - Seconds with trailing zeros

You can see all the date/time variables at PHP Date



Related Topics



Leave a reply



Submit