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
Calculate Total Sum of Dynamically Added Items to a Table
Increase Counter Value Upon Button Click
How to Make Autoplay of the Swiper Slider Start Only After the Slider Enters Viewport
Angular 4: Cannot Instantiate Cyclic Dependency! Injectiontoken_Http_Interceptors
Reduce the Build Time in a Large Angular 8
How to Take Screenshot of a Div With JavaScript
Executing Nodejs Script File in PHP Using Exec()
Check If an Array Is Empty in React Native
Jquery - Get Id Dynamically from Generated Element
How to Convert Uint8 Array to Base64 Encoded String
Sort Json Data Based on Date and Time
Why Flatlist Is Not Updating Dynamically in React Native
Property Does Not Exist on Type 'Never' Typescript/React
Access a Variable Outside of .Then Function
Filtering Item from Nested Object Array in Typescript
Making a JavaScript Function That Finds the First Word in a String/Sentence
How to Make Select Option Disable
Css Div Height Not Expanding to Fit Content or Wrapping Content