Convert Character String with AM & PM to Date & Time in R (Example)


This tutorial illustrates how to remove AM and PM from a date and time object in the R programming language.

The tutorial will contain this:

Let’s dive right into the example:


Creation of Example Data

The following data is used as a basis for this R tutorial:

my_dt <- c("5/02/2022 11:59:22 PM",    # Create example dates & times
           "7/12/2024 07:15:59 AM",
           "3/10/2022 10:12:03 PM",
           "5/09/2023 08:11:27 PM")
my_dt                                  # Print example dates & times
# [1] "5/02/2022 11:59:22 PM" "7/12/2024 07:15:59 AM" "3/10/2022 10:12:03 PM"
# [4] "5/09/2023 08:11:27 PM"

The previous output of the RStudio console shows that our example data is a vector containing four different dates and times. As you can see, our data contains AM and PM specifications.

Let’s also check the data class of our example vector:

class(my_dt)                           # Check class of data
# [1] "character"

Currently, our data is formatted as a character string.


Example: Drop AM & PM from Dates & Times Using parse_date_time() Function of lubridate Package

This example demonstrates how to transform a character string containing dates and times with AM and PM to a proper date and time object.

We first have to install and load the lubridate package:

install.packages("lubridate")          # Install & load lubridate package

In the next step, we can convert our character strings to dates and times without the AM and PM components.

For this, we have to specify %I for the hours, and %p for the AM and PM component:

my_dt_new <- parse_date_time(my_dt,    # Remove AM & PM from dates & times
                             "%d/%m/%y %I:%M:%S %p")
my_dt_new                              # Print updated dates
# [1] "2022-02-05 23:59:22 UTC" "2024-12-07 07:15:59 UTC"
# [3] "2022-10-03 22:12:03 UTC" "2023-09-05 20:11:27 UTC"

As you can see, the format of our date and time elements has been changed: The AM and PM components have been dropped.

Let’s check the class of our updated data:

class(my_dt_new)                       # Check class of data
# [1] "POSIXct" "POSIXt"

We have converted our character string to the POSIXct class. Looks good!


Video, Further Resources & Summary

Do you want to know more about the removal of AM and PM from a date and time object? Then you could watch the following video on my YouTube channel. I show the R syntax of this page in the video:


Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.


In addition, you may want to have a look at the other tutorials that I have published on


In this R tutorial you have learned how to drop AM and PM from a date and time. Let me know in the comments below, if you have additional comments and/or questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.