Home > Back-end >  How to convert "Mon, November 14, 2022 11:30 AM" to datetime in R?
How to convert "Mon, November 14, 2022 11:30 AM" to datetime in R?

Time:08-10

I need help trying to convert "Mon, November 14, 2022 11:30 AM" to datetime in R.

strftime(as.POSIXct(as.Date("Mon, November 14, 2022 11:30 AM")), format="%Y-%m-%d %H:%M:%S", tz="UCT")

I get the following error:

Error in charToDate(x) : 
  character string is not in a standard unambiguous format

CodePudding user response:

The format for abbreviated weekday is %a

as.POSIXct("Mon, November 14, 2022 11:30 AM",
      format = "%a, %b %d, %Y %H:%M %p")
[1] "2022-11-14 11:30:00 EST"

Or can do this automatically with parse_date

library(parsedate)
parse_date("Mon, November 14, 2022 11:30 AM")
[1] "2022-11-14 11:30:00 UTC"

CodePudding user response:

Another possible solution, based on lubridate::mdy_hm:

x <- "Mon, November 14, 2022 11:30 AM"

lubridate::mdy_hm(x)

#> [1] "2022-11-14 11:30:00 UTC"
  • Related