[R] Conversion of String to Datetime: How to Keep Timezone Offset when Printing?

Thomas Bulka thom@@@tb@bu|k@ @end|ng |rom gm@||@com
Fri Jun 4 20:20:07 CEST 2021

Hello list,

I do have a hard time handling date and time data with different
timezone offsets. Say, I have two strings which represent different
dates/times, like so:

DT1 <- "2021-06-19T13:45:00-03:00"
DT2 <- "2020-07-20T11:39:12+02:00"
my_dates <- c(DT1, DT2)

What I want to do now, is to covert those strings into some kind of
datetime object which allows for comparisons (earlier/later etc.).

Using as_datetime from the lubridate package seems to do the trick,
but it converts DT1 and DT2 with reference to UTC. I guess, this makes
sense from a computational point of view, since, most likely, this is
how R internally handles dates and times.

However, I would like to be able to at least visually distinguish the
different timezone offsets, if the data is printed. I'm able to
manually set a timezone with the tz parameter of as_datetime:

as_datetime(DT1, tz = "EDT")
[1] "2020-07-20 09:39:12 EDT"

But this only works if I enter the timezone manually.

So what I want to ask is this: Is there any possibility, to convert
all entries of a vector my_dates (see above) into datetime objects
while keeping a visual indication of each timezone offset when the
data is printed?

Please excuse any awkward phrasing; being pretty new to R, I probably
did not phrase anything correctly.

Thank you very much in advance!


More information about the R-help mailing list