0

I have a large dataset with many different dates, I want to convert them to POSIXct but do not care about times, all I need is for the time to be formatted identical between dates.

E.g. my original date format

2019-03-08 
2019-03-12 
2018-10-30 
2018-11-04 

when using the code data$newdate <- as.POSIXct(data$date,tz="UTC") becomes

2019-03-08 19:00:00 EST
2019-03-12 20:00:00 EDT
2018-10-30 20:00:00 EDT
2018-11-04 19:00:00 EST

I know that the times in the resulting format are technically identical, but I need them to be formatted as such too, but I can't figure out how. Is there a different timezone I should be using than UTC?

Beardedant
  • 134
  • 8
  • Does this answer your question? [Remove timezone during POSIXlt Conversion in R](https://stackoverflow.com/questions/44970127/remove-timezone-during-posixlt-conversion-in-r) – AndrewGB Mar 29 '22 at 21:34
  • I'm afraid not, the end product should be of the class POSIXct – Beardedant Mar 29 '22 at 21:35
  • 1
    Underneath the hood, those are all UTC times (at midnight) with output in your locale, so what you should have used to force a common output was`format`. Since `format` is generic it only stands to reason that there should be a `POSIXct` method. You were seeing the results of the `print.POSIXct` method which uses different arguments to `format` than what you wanted. – IRTFM Mar 30 '22 at 00:42

1 Answers1

1

This can be frustrating as the timezone data can enter when parsing, converting, and formating.

> library(anytime)
> d <- c("2019-03-08","2019-03-12","2018-10-30","2018-11-04")
> anydate(d)
[1] "2019-03-08" "2019-03-12" "2018-10-30" "2018-11-04"
> anytime(d)
[1] "2019-03-08 CST" "2019-03-12 CDT" "2018-10-30 CDT" "2018-11-04 CDT"
>
> # force text format
> format(anytime(d), "%Y-%m-%d %H:%M:%S")
[1] "2019-03-08 00:00:00" "2019-03-12 00:00:00" "2018-10-30 00:00:00" 
[4] "2018-11-04 00:00:00"
> 
> # or use another helper from anytime
> iso8601(anytime(d))
[1] "2019-03-08T00:00:00" "2019-03-12T00:00:00" "2018-10-30T00:00:00" 
[4] "2018-11-04T00:00:00"
>
Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • Seems odd to me that `as.POSIXct.Date` with `function(x, ...)` would call `.POSIXct(unclass(x) * 86400)` and not pass the `...` arguments through. It would work if the `tz=` wasn't ditched: `.POSIXct(unclass(x) * 86400, tz="UTC")`. I guess that's the sort of thing your package is trying to smooth out. – thelatemail Mar 29 '22 at 22:13
  • Yeah been there done that a few times myself and since `anytime` was written a few of the gotches when `tz` attributes do or do not get dropped where fixed in R. But it can still be a slippery slope. But `anytime()` and `anydate()` work for me, but hey, I wrote'em so ... – Dirk Eddelbuettel Mar 29 '22 at 22:17