Reputation: 150
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?
Upvotes: 0
Views: 202
Reputation: 368271
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"
>
Upvotes: 1