Convert 12Hour Time to 24Hour Time

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);

Convert 12 hour character time to 24 hour

This should work:

times <- c("9:06 AM", "4:42 PM", "3:05 PM", "12:00 PM", "3:38 AM")

strptime(times, "%I:%M %p")

## [1] "2015-04-23 09:06:00 EDT" "2015-04-23 16:42:00 EDT"
## [3] "2015-04-23 15:05:00 EDT" "2015-04-23 12:00:00 EDT"
## [5] "2015-04-23 03:38:00 EDT"

If you want just the times:

format(strptime(times, "%I:%M %p"), format="%H:%M:%S")
## [1] "09:06:00" "16:42:00" "15:05:00" "12:00:00" "03:38:00"

How to convert a 12 hour time stamp (NOT a datetime, time only) to 24 hour time?

Try

lab_data[,LAB_TM_test:=as.POSIXct(LAB_TM, format='%I:%M:%S %p')]

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

Convert 12 hour (AM/PM) string to 24 Date object using moment js

See documentation of moment js parse function

JSFiddle

var dt = moment("12:15 AM", ["h:mm A"]).format("HH:mm");

convert 12 hour time to 24 hour time in pandas dataframe

use pd.to_datetime to convert to datetime dtype, and cast to Python time objects via the dt accessor:

df['time'] = pd.to_datetime(df['time']).dt.time

# df['time']
# 0 22:30:00
# 1 23:30:00
# 2 01:20:00
# Name: time, dtype: object

...or add strftime to get a specific time string format:

df['time'] = pd.to_datetime(df['time']).dt.strftime('%H:%M')

# df['time']
# 0 22:30
# 1 23:30
# 2 01:20
# Name: time, dtype: object

Conversion from 12 hours time to 24 hours time in java

Try this:

import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
public static void main(String [] args) throws Exception {
SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
Date date = parseFormat.parse("10:30 PM");
System.out.println(parseFormat.format(date) + " = " + displayFormat.format(date));
}
}

which produces:

10:30 PM = 22:30

See: http://download.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html

How do you convert between 12 hour time and 24 hour time in PHP?

// 24-hour time to 12-hour time 
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));

// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));

How to convert 24 hour format to 12 hour format in python datetime.now()

you can use strftime function

from datetime import *
current_time = datetime.now().strftime("%I:%M %p")
print(current_time)


Related Topics



Leave a reply



Submit