Scott217
Scott217

Reputation: 133

Convert Character date/time with am and pm to date/time format

I am attempting to convert a character date time to a date/time format using strptime. The data is in MDY_HM (1/29/20 3:43pm).

My code currently looks like this:

comp_report_tz$Start_Date_Time <- strptime(comp_report_tz$Start_Date_Time, format = "%m/%d/%y %H:%M %p")

The output for each observation simply shows: "< POSIXlt >"

I don't receive any errors when executing.

Upvotes: 1

Views: 2038

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269586

Make the following changes:

  • use %I for the hour
  • use %p for the am/pm.
  • ensure that the format pattern is in the precise pattern of the data -- it's not in the question
  • you likely want POSIXct, not POSIXlt

thus we use this format

as.POSIXct("1/29/20 3:43pm", format = "%m/%d/%y %I:%M%p")
## [1] "2020-01-29 15:43:00 EST"

Upvotes: 4

Related Questions