Convert 12-Hour Hh:Mm Am/Pm to 24-Hour Hh:Mm

convert 12-hour hh:mm AM/PM to 24-hour hh:mm

Try this

var time = $("#starttime").val();
var hours = Number(time.match(/^(\d+)/)[1]);
var minutes = Number(time.match(/:(\d+)/)[1]);
var AMPM = time.match(/\s(.*)$/)[1];
if(AMPM == "PM" && hours<12) hours = hours+12;
if(AMPM == "AM" && hours==12) hours = hours-12;
var sHours = hours.toString();
var sMinutes = minutes.toString();
if(hours<10) sHours = "0" + sHours;
if(minutes<10) sMinutes = "0" + sMinutes;
alert(sHours + ":" + sMinutes);

How to convert AM/PM timestmap into 24hs format in Python?

Try this :)

Code:


currenttime = datetime.datetime.now().time().strftime("%H:%M")
if currenttime >= "10:00" and currenttime <= "13:00":
if m2 >= "10:00" and m2 >= "12:00":
m2 = ("""%s%s""" % (m2, " AM"))
else:
m2 = ("""%s%s""" % (m2, " PM"))
else:
m2 = ("""%s%s""" % (m2, " PM"))
m2 = datetime.datetime.strptime(m2, '%I:%M %p')
m2 = m2.strftime("%H:%M %p")
m2 = m2[:-3]
print m2

Output:


13:35

How to convert the time from AM/PM to 24 hour format in PHP?

Try with this

echo date("G:i", strtotime($time));

or you can try like this also

echo date("H:i", strtotime("04:25 PM"));

What is the algorithm represented in Java to convert an 12 hour am/pm meridiem format hour to 24 hour format?

This is my naive style code for beginners to convert an hour in 12 hour format to an hour in 24 format.

public static int convert12to24(String meridiem, String hour) {
//meridiem is that am or pm,
meridiem = meridiem.toLowerCase();
int h_12 = 0;
int h_24 = 0;
try {
h_12 = Integer.parseInt(hour);
} catch (NumberFormatException e) {
e.printStackTrace();
}
if (h_12 == 12) {
//this is the midnight hour
if (meridiem.contains("am")) {//generally before noon
h_24 = 0;
} else {//this is the hour starting at noon
h_24 = 12;
}
} else if (h_12 >= 1)//all the rest
{
if (meridiem.contains("am")) {
//hour starting after first hour at midnight to 11 facing noon
h_24 = h_12;
} else {//pm hours starting right after first hour after noon
h_24 = h_12 + 12;
}
}

return h_24;
}

Converting 12 Hour to 24 Hour time format in Pandas

Use format parameter with %I:%M:%S %p - here %I is for hours in 12H format and %p for match AM or PM:

df['Time'] = pd.to_datetime(df['Time'], format='%I:%M:%S %p').dt.strftime('%H:%M:%S')
print (df)
Time
1 17:21:26
2 17:21:58
3 17:22:22
4 17:22:36
5 19:18:16


Related Topics



Leave a reply



Submit