# How to convert 12-hour time scale to 24-hour time in R?

Dealing with time data is not an easy task, it is sometimes difficult even in built-in analytical softwares, thus it won’t be easy in R as well. Mostly, we record time on a 12-hour time scale but in some situations, we need 24-hour time scale. Therefore, if we want to convert 12-hour time scale to 24-hour time scale then format function can be used with as.POSIXct. Look at the below examples, to understand it better.

## Example1

Live Demo

Time1<−c("12:00 AM","12:42 AM","2:15 PM","11:01 AM","4:20 AM","6:11 AM","7:42 PM","5:17 AM","10:10 PM")
Time1
[1] "12:00 AM" "12:42 AM" "2:15 PM" "11:01 AM" "4:20 AM" "6:11 AM" "7:42 PM"
[8] "5:17 AM" "10:10 PM"
format(as.POSIXct(Time1,format='%I:%M %p'),format="%H:%M:%S")
[1] "00:00:00" "00:42:00" "14:15:00" "11:01:00" "04:20:00" "06:11:00" "19:42:00"
[8] "05:17:00" "22:10:00"

## Example2

Live Demo

Time2<−sample(c("12:00 AM","12:42 AM","2:15 PM","11:01 AM","4:20 AM","6:11 AM","7:42 PM","5:17 AM","10:10 PM"),50,replace=TRUE)
Time2
[1] "4:20 AM" "2:15 PM" "10:10 PM" "12:42 AM" "5:17 AM" "10:10 PM"
[7] "11:01 AM" "6:11 AM" "7:42 PM" "4:20 AM" "4:20 AM" "11:01 AM"
[13] "11:01 AM" "5:17 AM" "12:00 AM" "12:42 AM" "10:10 PM" "6:11 AM"
[19] "12:00 AM" "11:01 AM" "5:17 AM" "12:42 AM" "7:42 PM" "5:17 AM"
[25] "7:42 PM" "5:17 AM" "5:17 AM" "4:20 AM" "5:17 AM" "10:10 PM"
[31] "4:20 AM" "2:15 PM" "7:42 PM" "6:11 AM" "2:15 PM" "11:01 AM"
[37] "10:10 PM" "2:15 PM" "12:42 AM" "12:00 AM" "11:01 AM" "5:17 AM"
[43] "6:11 AM" "11:01 AM" "5:17 AM" "4:20 AM" "5:17 AM" "5:17 AM"
[49] "4:20 AM" "2:15 PM"
format(as.POSIXct(Time2,format='%I:%M %p'),format="%H:%M:%S")
[1] "04:20:00" "14:15:00" "22:10:00" "00:42:00" "05:17:00" "22:10:00"
[7] "11:01:00" "06:11:00" "19:42:00" "04:20:00" "04:20:00" "11:01:00"
[13] "11:01:00" "05:17:00" "00:00:00" "00:42:00" "22:10:00" "06:11:00"
[19] "00:00:00" "11:01:00" "05:17:00" "00:42:00" "19:42:00" "05:17:00"
[25] "19:42:00" "05:17:00" "05:17:00" "04:20:00" "05:17:00" "22:10:00"
[31] "04:20:00" "14:15:00" "19:42:00" "06:11:00" "14:15:00" "11:01:00"
[37] "22:10:00" "14:15:00" "00:42:00" "00:00:00" "11:01:00" "05:17:00"
[43] "06:11:00" "11:01:00" "05:17:00" "04:20:00" "05:17:00" "05:17:00"
[49] "04:20:00" "14:15:00"

## Example3

Live Demo

Time3<−sample(c("4:05 AM","1:40 AM","12:15 PM","11:21 AM","4:23 AM","6:45 AM","8:20 AM","5:17 PM","10:33 PM"),50,replace=TRUE)
Time3
[1] "11:21 AM" "12:15 PM" "6:45 AM" "11:21 AM" "1:40 AM" "11:21 AM"
[7] "1:40 AM" "4:05 AM" "8:20 AM" "4:23 AM" "12:15 PM" "1:40 AM"
[13] "1:40 AM" "8:20 AM" "11:21 AM" "10:33 PM" "4:23 AM" "4:23 AM"
[19] "6:45 AM" "4:23 AM" "8:20 AM" "4:23 AM" "1:40 AM" "10:33 PM"
[25] "6:45 AM" "10:33 PM" "4:05 AM" "10:33 PM" "8:20 AM" "4:23 AM"
[31] "6:45 AM" "11:21 AM" "12:15 PM" "12:15 PM" "10:33 PM" "8:20 AM"
[37] "4:23 AM" "8:20 AM" "12:15 PM" "6:45 AM" "1:40 AM" "6:45 AM"
[43] "12:15 PM" "6:45 AM" "4:23 AM" "1:40 AM" "11:21 AM" "11:21 AM"
[49] "8:20 AM" "8:20 AM"
format(as.POSIXct(Time3,format='%I:%M %p'),format="%H:%M:%S")
[1] "11:21:00" "12:15:00" "06:45:00" "11:21:00" "01:40:00" "11:21:00"
[7] "01:40:00" "04:05:00" "08:20:00" "04:23:00" "12:15:00" "01:40:00"
[13] "01:40:00" "08:20:00" "11:21:00" "22:33:00" "04:23:00" "04:23:00"
[19] "06:45:00" "04:23:00" "08:20:00" "04:23:00" "01:40:00" "22:33:00"
[25] "06:45:00" "22:33:00" "04:05:00" "22:33:00" "08:20:00" "04:23:00"
[31] "06:45:00" "11:21:00" "12:15:00" "12:15:00" "22:33:00" "08:20:00"
[37] "04:23:00" "08:20:00" "12:15:00" "06:45:00" "01:40:00" "06:45:00"
[43] "12:15:00" "06:45:00" "04:23:00" "01:40:00" "11:21:00" "11:21:00"
[49] "08:20:00" "08:20:00"

## Example4

Live Demo

Time4<−sample(c("1:05 PM","12:40 PM","12:25 PM","11:21 PM","4:13 PM","6:07 PM","8:29 PM","4:30 PM","8:11 PM"),50,replace=TRUE)
Time4
[1] "8:11 PM" "12:40 PM" "11:21 PM" "11:21 PM" "12:40 PM" "12:40 PM"
[7] "6:07 PM" "12:40 PM" "12:40 PM" "11:21 PM" "6:07 PM" "4:13 PM"
[13] "4:30 PM" "4:30 PM" "1:05 PM" "4:30 PM" "8:29 PM" "12:25 PM"
[19] "8:29 PM" "8:11 PM" "8:11 PM" "8:11 PM" "12:25 PM" "4:13 PM"
[25] "8:11 PM" "8:29 PM" "6:07 PM" "8:11 PM" "6:07 PM" "4:13 PM"
[31] "8:11 PM" "4:13 PM" "6:07 PM" "8:11 PM" "12:40 PM" "4:13 PM"
[37] "12:40 PM" "12:40 PM" "4:13 PM" "4:30 PM" "6:07 PM" "12:25 PM"
[43] "4:13 PM" "1:05 PM" "12:25 PM" "12:25 PM" "1:05 PM" "12:25 PM"
[49] "4:13 PM" "6:07 PM"
format(as.POSIXct(Time4,format='%I:%M %p'),format="%H:%M:%S")
[1] "20:11:00" "12:40:00" "23:21:00" "23:21:00" "12:40:00" "12:40:00"
[7] "18:07:00" "12:40:00" "12:40:00" "23:21:00" "18:07:00" "16:13:00"
[13] "16:30:00" "16:30:00" "13:05:00" "16:30:00" "20:29:00" "12:25:00"
[19] "20:29:00" "20:11:00" "20:11:00" "20:11:00" "12:25:00" "16:13:00"
[25] "20:11:00" "20:29:00" "18:07:00" "20:11:00" "18:07:00" "16:13:00"
[31] "20:11:00" "16:13:00" "18:07:00" "20:11:00" "12:40:00" "16:13:00"
[37] "12:40:00" "12:40:00" "16:13:00" "16:30:00" "18:07:00" "12:25:00"
[43] "16:13:00" "13:05:00" "12:25:00" "12:25:00" "13:05:00" "12:25:00"
[49] "16:13:00" "18:07:00"

## Example5

Live Demo

Time5<−sample(c("11:05 PM","10:40 PM","10:25 PM","8:21 PM"),50,replace=TRUE)
Time5
[1] "10:25 PM" "8:21 PM" "10:40 PM" "11:05 PM" "10:40 PM" "10:40 PM"
[7] "10:40 PM" "10:25 PM" "10:40 PM" "10:25 PM" "10:40 PM" "10:40 PM"
[13] "8:21 PM" "10:40 PM" "10:25 PM" "10:40 PM" "11:05 PM" "11:05 PM"
[19] "10:40 PM" "10:25 PM" "10:25 PM" "11:05 PM" "10:25 PM" "8:21 PM"
[25] "11:05 PM" "10:40 PM" "11:05 PM" "11:05 PM" "11:05 PM" "10:25 PM"
[31] "10:40 PM" "8:21 PM" "11:05 PM" "8:21 PM" "10:25 PM" "8:21 PM"
[37] "10:25 PM" "11:05 PM" "11:05 PM" "10:25 PM" "10:25 PM" "10:40 PM"
[43] "10:40 PM" "8:21 PM" "8:21 PM" "8:21 PM" "8:21 PM" "11:05 PM"
[49] "10:40 PM" "11:05 PM"
format(as.POSIXct(Time5,format='%I:%M %p'),format="%H:%M:%S")
[1] "22:25:00" "20:21:00" "22:40:00" "23:05:00" "22:40:00" "22:40:00"
[7] "22:40:00" "22:25:00" "22:40:00" "22:25:00" "22:40:00" "22:40:00"
[13] "20:21:00" "22:40:00" "22:25:00" "22:40:00" "23:05:00" "23:05:00"
[19] "22:40:00" "22:25:00" "22:25:00" "23:05:00" "22:25:00" "20:21:00"
[25] "23:05:00" "22:40:00" "23:05:00" "23:05:00" "23:05:00" "22:25:00"
[31] "22:40:00" "20:21:00" "23:05:00" "20:21:00" "22:25:00" "20:21:00"
[37] "22:25:00" "23:05:00" "23:05:00" "22:25:00" "22:25:00" "22:40:00"
[43] "22:40:00" "20:21:00" "20:21:00" "20:21:00" "20:21:00" "23:05:00"
[49] "22:40:00" "23:05:00"

Updated on: 06-Nov-2020

2K+ Views