strptime in R: How to Convert Character to Time in R

To format dates in R, use the as.Date() function. To format dates with times, use the strptime() method. Functions to convert between character representations and objects of classes "POSIXlt" and "POSIXct" representing calendar dates and times.

strptime in R

The strptime() is a built-in R function that converts characters to time objects. The strptime() method turns character representations into an object of class “POSIXlt“.

Syntax

strptime(x, format, tz = "")

Parameters

x: It is an object to be converted: a character vector for strptime, an object converted to “POSIXlt” for strftime.

format: It is a character string. The default for the format methods is “%Y-%m-%d %H:%M:%S” if any element has a time component that is not midnight, and “%Y-%m-%d” otherwise. If the options(“digits.secs”) argument is set, up to the specified number of digits will be printed for seconds.

tz: It is a character string specifying the time zone to be used for the conversion. System-specific (see as.POSIXlt), but ” “ is the current time zone, and “GMT” is UTC. Invalid values are most commonly treated as UTC on some platforms with a warning.

Example

Let’s define a character object in R.

today <- "2021-03-30"

Let’s determine the class of today variable using the class() method.

today <- "2021-03-30"
class(today)

Output

[1] "character"

You can see that we defined a variable of type character.

Use the strptime() function to transform this character string into a time object.

today <- "2021-03-30"
time_obj <- strptime(today, format = "%Y-%m-%d")
time_obj

Output

[1] "2021-03-30 IST"

As you can see from the output is that it is showing the time zone abbreviation IST (i.e., Indian Standard Time).

Let’s check the class of our converted data using the class() function.

today <- "2021-03-30"
time_obj <- strptime(today, format = "%Y-%m-%d")
class(time_obj)

Output

[1] "POSIXlt" "POSIXt"

The POSIXlt and POSIXt are the two typical time classes of the R.

Passing timezone as an argument

To set up a timezone in R using the strptime() method, pass the tz argument. The strptime() method accepts three arguments, and one of them is tz means timezone.

today <- "2021-03-30"
time_obj <- strptime(today, format = "%Y-%m-%d", tz = "EST")
time_obj

Output

[1] "2021-03-30 EST"

And in the output, we are getting the time object of the Eastern Time Zone.

Getting time object with hours, mins, and secs in R

Let’s extend the character object by days, hours, minutes and then use the strptime() to convert it into a time object.

today <- "2021-03-30 19:21:11"

Here, we defined a character object with extended days, hours, and minutes.

today <- "2021-03-30 19:21:11"
time_obj <- strptime(today, format = "%Y-%m-%d %H:%M:%OS", tz = "EST")
time_obj

Output

[1] "2021-03-30 19:21:11 EST"

Passing Time Object With Milliseconds

Let’s extend the character object again by adding milliseconds and then use the strptime() to convert it into a time object.

today <- "2021-03-30 19:21:11.150"

To add milliseconds to a time object, we have to modify the format option.

today <- "2021-03-30 19:21:11.150"
time_obj <- strptime(today, format = "%Y-%m-%d %H:%M:%OS", tz = "EST")
time_obj

Output

[1] "2021-03-30 19:21:11 EST"

You can see from the output that it is no different than the previous one. That means we are missing something here.

The default options of R are set to show 0 millisecond digits. For that reason, we have to update the global R options.

options(digits.secs = 3)

Now rerun the above program and observe the output.

options(digits.secs = 3)

today <- "2021-03-30 19:21:11.150"
time_obj <- strptime(today, format = "%Y-%m-%d %H:%M:%OS", tz = "EST")
time_obj

Output

[1] "2021-03-30 19:21:11.15 EST"

You can see from the output that .15 millisecond is there. Don’t forget to set the global options back to zero milliseconds, in case you don’t require to use the modified global options for the rest of your R session.

Final Words

To convert characters to time objects in R, use the strptime() function. To convert time objects to characters in R, use the strftime() function.

That is it for strptime() Function in R tutorial.

Leave a Comment