Javascript: convert 24-hour time-of-day string to 12-hour time with AM/PM and no timezone
Nothing built in, my solution would be as follows :
function 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');
This function uses a regular expression to validate the time string and to split it into its component parts. Note also that the seconds in the time may optionally be omitted.
If a valid time was presented, it is adjusted by adding the AM/PM indication and adjusting the hours.
The return value is the adjusted time if a valid time was presented or the original string.
Working example
(function() {
function 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 }
var tel = document.getElementById('tests');
tel.innerHTML = tel.innerHTML.split(/\r*\n|\n\r*|\r/).map(function(v) { return v ? v + ' => "' + tConvert(v.trim()) + '"' : v; }).join('\n');})();
<h3>tConvert tests : </h3><pre id="tests"> 18:00:00 18:00 00:00 11:59:01 12:00:00 13:01:57 24:00 sdfsdf 12:61:54</pre>
Converting 24 hour time to 12 hour time w/ AM & PM using Javascript
UPDATE 2: without seconds option
UPDATE: AM after noon corrected, tested: http://jsfiddle.net/aorcsik/xbtjE/
I created this function to do this:
function formatDate(date) { var d = new Date(date); var hh = d.getHours(); var m = d.getMinutes(); var s = d.getSeconds(); var dd = "AM"; var h = hh; if (h >= 12) { h = hh - 12; dd = "PM"; } if (h == 0) { h = 12; } m = m < 10 ? "0" + m : m;
s = s < 10 ? "0" + s : s;
/* if you want 2 digit hours: h = h<10?"0"+h:h; */
var pattern = new RegExp("0?" + hh + ":" + m + ":" + s);
var replacement = h + ":" + m; /* if you want to add seconds replacement += ":"+s; */ replacement += " " + dd;
return date.replace(pattern, replacement);}
alert(formatDate("February 04, 2011 12:00:00"));
How do I convert date/time from 24-hour format to 12-hour AM/PM?
I think you can use date()
function to achive this
$date = '19:24:15 06/13/2013';
echo date('h:i:s a m/d/Y', strtotime($date));
This will output
07:24:15 pm 06/13/2013
Live Sample
h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
m is used for months with digits
d is used for days in digit
Y uppercase is used for 4 digit year (use it lowercase for two digit)
Updated
This is with DateTime
$date = new DateTime('19:24:15 06/13/2013');
echo $date->format('h:i:s a m/d/Y') ;
Live Sample
How to Convert Time from 24 hour format to 12 hour format (AM/PM) with Pandas (Python)
Try with .dt
instead of .datetime
per your last line of code
df['start'] = pd.to_datetime(df['start']).dt.strftime('%I:%M %p')
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.dt.html
How can I convert 24 hour time to 12 hour time?
>>> from datetime import datetime
>>> d = datetime.strptime("10:30", "%H:%M")
>>> d.strftime("%I:%M %p")
'10:30 AM'
>>> d = datetime.strptime("22:30", "%H:%M")
>>> d.strftime("%I:%M %p")
'10:30 PM'
Display current time in 12 hour format with AM/PM
Easiest way to get it by using date pattern - h:mm a
, where
- h - Hour in am/pm (1-12)
- m - Minute in hour
- a - Am/pm marker
Code snippet :
DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
Read more on documentation - SimpleDateFormat java 7
How to convert character date time if time mixed between 12 hour and 24 hour time?
Try lubridate
, it is a very robust package for dates and times:
library(lubridate)
ymd_hms(c("2019-01-20 10:25:00 PM", "2019-01-20 22:25:00"))
[1] "2019-01-20 22:25:00 UTC" "2019-01-20 22:25:00 UTC"
Check ?ymd_hms
for more options.
Related Topics
How to Make My PHP Ide Understand Dependency Injection Containers
PHP Sessions Login with Remember Me
Check If Current User Is Administrator in Wordpress
Reading Ssl Page with Curl (Php)
Regex Ignore Url Already in HTML Tags
Windows 7 PHP + Symfony2 Terribly Slow
Pdo Multiple Named Placeholders Doesnt Retrieve Data
Environment Driven Database Settings in Laravel
Have Trouble Installing PHPmyadmin on PHP7 Apache/2.4.7 (Ubuntu)
Tracking Email with PHP and Image
Mssql Query Issue in PHP and Querying Text Data
Strategy to Override a Class in a Library Installed with Composer
How to Convert the Time from Am/Pm to 24 Hour Format in PHP
How to Force Formrequest Return JSON in Laravel 5.1