0

I have problem with a character in form: 01JAN2018:00:00:00 I want to get 2018_01_01 as result.

I tried this code but that returns NA

as.POSIXct("01JAN2018:00:00:00", format = "%d-%m-%Y")

thank you

DD chen
  • 169
  • 11
  • 2
    there is no `-` in the string. You need `as.POSIXct("01JAN2018:00:00:00", format = "%d%b%Y")` – akrun Jan 22 '19 at 17:58
  • unfortunately, that didn't work for me, it returns NA. is there a problem with as.POSIXct? – DD chen Jan 22 '19 at 18:10
  • @DDchen ensure you are using `%b` and not `%m` – Chabo Jan 22 '19 at 18:13
  • @Chabo yes, i tried exactly what akrun suggested, that's weird – DD chen Jan 22 '19 at 18:17
  • using lubridate you can just use `dmy_hms('01JAN2018:00:00:00')` ... or if you want to just convert it to a date (and not a datetime object), you can do `as_date(dmy_hms('01JAN2018:00:00:00'))` – Dave Rosenman Jan 22 '19 at 18:19
  • thank you all, i tried many suggestions and I tried to set the local time with: Sys.setlocale("LC_TIME","C") and then thats works with as.POSIXct. – DD chen Jan 23 '19 at 13:49

0 Answers0