as.POSIXlt Function in R (2 Examples)
In this R tutorial you’ll learn how to manipulate date and time objects using the as.POSIXlt function.
Table of contents:
Let’s do this:
Example 1: Apply as.POSIXlt() Function with Default Specifications
Example 1 explains the basic application of the as.POSIXlt function.
For this, we first have to create an exemplifying date and time object in R:
x1 <- "2025-10-22 07:32:59" # Create example date & time x1 # Print example date & time #  "2025-10-22 07:32:59"
As you can see, our example data object contains a single element that represents the 22nd of October 2025 and 07 hours, 32 minutes, and 59 seconds.
However, at this time our data object is not properly formatted as a date and time object, as you can see by applying the class function to our data object:
class(x1) # Check class of data object #  "character"
The class function returns the data type “character”.
If we want to convert this character to the POSIXlt class, we can apply the as.POSIXlt function as shown below:
x1_POSIX <- as.POSIXlt(x1) # Convert class to POSIXlt x1_POSIX # Print POSIXlt object #  "2025-10-22 07:32:59 UTC"
The previous output has slightly changed: You can see that a time zone was added to the date and time object (i.e. UTC).
However, the even more important change gets visible by applying the class function once again:
class(x1_POSIX) # Check class of data object #  "POSIXlt" "POSIXt"
Our new data object has the POSIXlt class!
Example 2: Apply as.POSIXlt() Function with Manually Specified Format
In Example 2, I’ll illustrate how to manipulate date and time objects that are not formatted properly.
For this, we first have to create another data object in R:
x2 <- "22-10-2025 07:32" # Create example date & time x2 # Print example date & time #  "22-10-2025 07:32"
Our new data object contains the same date as the data object that we have used in Example 1. However, this time the ordering of the day, month, and year is different; and our data object contains only hours and minutes, but no seconds.
If we now apply the as.POSIXlt command to this data object, the output is wrong:
x2a_POSIX <- as.POSIXlt(x2) # as.POSIXlt without specific format x2a_POSIX # Wrong output #  "22-10-20 UTC"
In order to handle such data objects, we have to specify the format of the date and time within the as.POSIXlt function:
x2b_POSIX <- as.POSIXlt(x2, # Specify format argument properly format = "%d-%m-%Y %H:%M") x2b_POSIX # Correct output #  "2025-10-22 07:32:00 UTC"
The new output looks good!
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. In the video, I’m demonstrating the R programming codes of this article.
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.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you may have a look at the other tutorials on my website. I have released several tutorials already:
- as.Date Function in R
- Convert Year, Month, Day, Hour, Minutes & Seconds to Date & Time
- Change Default Time Zone in R
- How to Split a Date-Time Column into Separate Variables
- Add Time to Date Object in R
- Time Difference Between Dates in Weeks, Days, Hours, Minutes & Seconds
- Extract Hours, Minutes & Seconds from Date & Time Object
- Important Commands in R
- The R Programming Language
You have learned in this tutorial how to apply the as.POSIXlt function in the R programming language.
Please note that the as.POSIXct function can be applied the same way as the as.POSIXlt function.
If you have further questions, please let me know in the comments section.