Charles Stangor
Charles Stangor

Reputation: 304

How to read date time string with full weekday and month names?

So I'm trying to read a date from a long date string.

I'm not real sure what I'm doing wrong here.

lines = "Friday, November 30, 2018, 8:00 AM"
as.POSIXlt(lines, format = "%A, %B %m, %Y, %I, %p")

Upvotes: 4

Views: 187

Answers (2)

agstudy
agstudy

Reputation: 121608

Using lubridate package:

parse_date_time(lines, "%A, %B %d, %Y, %I:%M %p")

or

parse_date_time(lines, "%A, %B %d, %Y, %H:%M %p")

## "2018-11-30 08:00:00 UTC"

or even simpler format :

parse_date_time(lines, "abdyHMp")

Upvotes: 2

zx8754
zx8754

Reputation: 56249

Try this:

# example dates
lines = c("Friday, November 30, 2018, 8:00 AM",
          "Friday, November 30, 2018, 8:00 PM")

as.POSIXlt(lines, format = "%A, %B %d, %Y, %I:%M %p")
# "2018-11-30 08:00:00 GMT"
# "2018-11-30 20:00:00 GMT"

Upvotes: 3

Related Questions