Change 24 hour time format to 12 hour time without dates

Look at the observations of columns "Dep_Time" and "Arrival_Time." As I tried to find similar issues, the topics I found had dates. I want to change them to a 12-hour format without AM/PM or dates. If you can help me with this, I would greatly appreciate it.

Please post some of your data. You can do that by posting the output of

dput(head(data, 20))

where data is the name of your data frame. Put a line with three back ticks just before and after the output, like this:
```
Pasted output goes here
```
What will you use these columns for? It may not be possible to display time-of-day in a 12 hour format and also preserve its numeric value, i.e. seconds since midnight.

dput(head(data, 20))


structure(list(Airline = structure(c(4L, 2L, 5L, 4L, 4L, 9L, 
5L, 5L, 5L, 7L, 2L, 4L, 2L, 5L, 4L, 2L, 9L, 5L, 2L, 5L), levels = c("Air Asia", 
"Air India", "GoAir", "IndiGo", "Jet Airways", "Jet Airways Business", 
"Multiple carriers", "Multiple carriers Premium economy", "SpiceJet", 
"Trujet", "Vistara", "Vistara Premium economy"), class = "factor"), 
    Date_of_Journey = c("03/24/2019", "05/01/2019", "06/09/2019", 
    "05/12/2019", "03/01/2019", "06/24/2019", "03/12/2019", "03/01/2019", 
    "03/12/2019", "05/27/2019", "06/01/2019", "04/18/2019", "06/24/2019", 
    "05/09/2019", "04/24/2019", "03/03/2019", "04/15/2019", "06/12/2019", 
    "06/12/2019", "05/27/2019"), Source = structure(c(1L, 4L, 
    3L, 4L, 1L, 4L, 1L, 1L, 1L, 3L, 3L, 4L, 2L, 4L, 4L, 3L, 3L, 
    3L, 3L, 3L), levels = c("Banglore", "Chennai", "Delhi", "Kolkata", 
    "Mumbai"), class = "factor"), Destination = structure(c(6L, 
    1L, 2L, 1L, 6L, 1L, 6L, 6L, 6L, 2L, 2L, 1L, 5L, 1L, 1L, 2L, 
    2L, 2L, 2L, 2L), levels = c("Banglore", "Cochin", "Delhi", 
    "Hyderabad", "Kolkata", "New Delhi"), class = "factor"), 
    Route = c("BLR → DEL", "CCU → IXR → BBI → BLR", "DEL → LKO → BOM → COK", 
    "CCU → NAG → BLR", "BLR → NAG → DEL", "CCU → BLR", 
    "BLR → BOM → DEL", "BLR → BOM → DEL", "BLR → BOM → DEL", 
    "DEL → BOM → COK", "DEL → BLR → COK", "CCU → BLR", 
    "MAA → CCU", "CCU → BOM → BLR", "CCU → BLR", "DEL → AMD → BOM → COK", 
    "DEL → PNQ → COK", "DEL → BOM → COK", "DEL → CCU → BOM → COK", 
    "DEL → BOM → COK"), Dep_Time = c("22:20", "05:50", "09:25", 
    "18:05", "16:50", "09:00", "18:55", "08:00", "08:55", "11:25", 
    "09:45", "20:20", "11:40", "21:10", "17:15", "16:40", "08:45", 
    "14:00", "20:15", "16:00"), Arrival_Time = c("01:10 22 Mar", 
    "13:15", "04:25 10 Jun", "23:30", "21:35", "11:25", "10:25 13 Mar", 
    "05:05 02 Mar", "10:25 13 Mar", "19:15", "23:00", "22:55", 
    "13:55", "09:20 10 May", "19:50", "19:15 04 Mar", "13:15", 
    "12:35 13 Jun", "19:15 13 Jun", "12:35 28 May"), Duration = c("2h 50m", 
    "7h 25m", "19h", "5h 25m", "4h 45m", "2h 25m", "15h 30m", 
    "21h 5m", "25h 30m", "7h 50m", "13h 15m", "2h 35m", "2h 15m", 
    "12h 10m", "2h 35m", "26h 35m", "4h 30m", "22h 35m", "23h", 
    "20h 35m"), Total_Stops = structure(c(5L, 2L, 2L, 1L, 1L, 
    5L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 1L, 5L, 2L, 1L, 1L, 2L, 1L
    ), levels = c("1 stop", "2 stops", "3 stops", "4 stops", 
    "non-stop"), class = "factor"), Additional_Info = structure(c(8L, 
    8L, 8L, 8L, 8L, 8L, 6L, 8L, 6L, 8L, 8L, 8L, 8L, 6L, 8L, 8L, 
    8L, 6L, 8L, 6L), levels = c("1 Long layover", "1 Short layover", 
    "2 Long layover", "Business class", "Change airports", "In-flight meal not included", 
    "No check-in baggage included", "No info", "No Info", "Red-eye flight"
    ), class = "factor"), Price = c(3897, 7662, 13882, 6218, 
    13302, 3873, 11087, 22270, 11087, 8625, 8907, 4174, 4667, 
    9663, 4804, 14011, 5830, 10262, 13381, 12898)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))

I retract my statement about not adding AM/PM. However, I still want to change these times to a 12-hour format with AM and PM.

This can be done with formattable.
For example

library(formattable)

# Sample vector of time values
times <- c("09:30:00", "13:45:00", "20:15:00", "03:00:00")

# Convert times to POSIXlt format for easier formatting
times_posix <- as.POSIXlt(times, format = "%H:%M:%S")

# Define a formatting function to convert POSIXlt format to clock format
clock_format <- function(x) {
  format(x, format = "%I:%M %p")
}

# Use formattable to apply the clock formatting to the vector of times
(ftimes <- formattable(times_posix, 
                              formatter =  clock_format))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.