This article describes how it is possible to convert a date from UTC time to a different time zone:
Requirements
- A document that contains a date variable that is being stored in UTC time.
Please note these steps require a Displayr license.
Method
- Hover over any variable in your Data Sources tree > Plus (+) > Insert Variable(s) > Custom Code > R - Text.
- Paste the code below into the R CODE box of the object inspector to convert the DateTime variable from UTC to Sydney AEST time.
library(lubridate)
date = force_tzs(`DateTime`, tzones = "UTC")
strftime(date, format = "%Y/%m/%d %H:%M", tz = "Australia/Sydney")
The first line is where we call the lubridate library. As 'DateTime' is already set up as a recognizable date/time variable in Displayr, we can simply use force_tzs to append the UTC time zone to our date without the need to format it first.
The last step is to set it back to a string using strftime in the same standard format as before (YYYY/MM/DD HH:MM) but with the specified time zone (tz) set to local Sydney time (AEST). A list of the time zone names that dates can be converted to can be found here. The advantage of using lubridate's functions instead of arbitrarily adding the current 11-hour difference between time zones is that it automatically takes into account Daylight Savings.
- Finally, to change this text variable back into a date variable, we simply need to go to the object inspector and under Data > Properties > Structure select Date/Time from the drop-down.
Next
How to Work with Aggregated Dates Using R