1

I am currently working with a data set with a million rows where all of the dates are not in the standard unambiguous format that R requires. I have worked on trying to reformat the date-time to the standard unambiguous format but have had zero luck. I have tried using functions such as format, as.POSIXlt and as.POSIXct but with no luck.

The format is currently "%m/%d/%Y %H:%M:%OS" and I need to reformat it as "%Y/%m/%d %H:%M:%OS".

Any help is appreicated.

Cole
  • 23
  • 4
  • 2
    R does not render/print times (and dates) as whatever format you want: it _always_ renders a timestamp as `%Y-%m-%d %H:%M:%S` (where `%S` can be influenced using `options(digits.secs=3)` or similar). If you want dates or timestamps to look other than that format, you must convert them into strings (using `format(.., format="%Y/%m/%d ...")`) and then never do number-like operations on them again (adding/subtracting days, etc). – r2evans Nov 22 '22 at 21:00
  • 2
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. What exactly did you try? What errors/problems did you have? – MrFlick Nov 22 '22 at 21:00
  • 3
    If you want it rendered as `%Y/%m/%d` in a report, then I suggest you keep it as the default R `POSIXct`- (or `Date`-) object (that will show as `%Y-%m-%d`) up until you do the final table/plot rendering, at which point you change it at the last moment. – r2evans Nov 22 '22 at 21:01

1 Answers1

0

Using the lubridate package from tidyverse, you can readily convert to your desired format.

library(lubridate)
oldformat <- as.character("10/13/2022 01:12:33")
newformat <- mdy_hms(oldformat)
Kate
  • 51
  • 1
  • 3