convert 24 hours to 12 hours javascript
var suffix = hour >= 12 ? "PM":"AM";
var hours = ((hour + 11) % 12 + 1) + suffixfunction tConvert (time) {
// Check correct time format and split into components
time = time.toString ().match (/^([01]\d|2[0-3])(:)([0-5]\d)(:[0-5]\d)?$/) || [time];
if (time.length > 1) { // If time format correct
time = time.slice (1); // Remove full string match value
time[5] = +time[0] < 12 ? 'AM' : 'PM'; // Set AM/PM
time[0] = +time[0] % 12 || 12; // Adjust hours
}
return time.join (''); // return adjusted time or original string
}
tConvert ('18:00:00');
Also in JavaScript:
- javascript continue
- curl post json object command
- What are the decodeURI() and encodeURI()
- is checked checkbox jquery
- json stringify recursive
- react if event.target is input
- javascript type casting int
- javascript vererbung Klasse extends super constructor
- new instance of object javascript
- js get last element of array
- how to loop through an array of objects+
- Javascript code for Age calculator chatbot
- redirect to link using jquery on new tab
- what is npm global install
- next js typescript
- react native curved view
- Syntax Error: Thread Loader (Worker 0) .eslintrc.js: Environment key "es2021" is unknown at Array.forEach (<anonymous>)
- mysql compare work days
- array fill
- types of functions in javascript
- hmac_sha256 node
- angular stoppropagatio
- ajax
- javascript substring vs substr