ilyas
ilyas

Reputation: 629

How to convert string to date format in R

I have a column of strings in the following format:

Wed, 6 Dec 2000 08:47:00 -0800 (PST)

How can I convert this into date format using lubridate or another package? I have done this before, but there was no -0800 (PST) at the end.

Thank you.

Upvotes: 1

Views: 1321

Answers (2)

akrun
akrun

Reputation: 886938

We can also use parse_date_time

library(lubridate)
parse_date_time(x, "adbY HMS z", tz = "US/Pacific")
#[1] "2000-12-06 08:47:00 PST"

Upvotes: 1

Tim Biegeleisen
Tim Biegeleisen

Reputation: 520878

I was able to get a result using strptime() without even worrying about the timezone name at the end:

> x - "Wed, 6 Dec 2000 08:47:00 -0800 (PST)"
> strptime(x,  "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"

However, if you want to remove the timezone name, you can use substr() to do this:

> strptime(substr(x, 1, nchar(x)-6),  "%a, %d %b %Y %H:%M:%S %z")
[1] "2000-12-07 00:47:00"

Upvotes: 3

Related Questions