Introduction
Working with dates and times can be challenging to say the least, especially when they are in the wrong time zone or format, or you want to manipulate them in some other way. Thankfully, this is made easy in Displayr using an extremely helpful R package called lubridate.
Depending on how the data was collected, the date format may not be ideal. Sometimes the date may be in a different format (i.e., month–day–year instead of day/month/year). And sometimes it may be stored in UTC (Coordinated Universal Time) or the hosted server time zone instead of the local time set on your computer.
Luckily there are ways to convert dates into different time zones in Displayr without a lot of hassle. 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.
Method
First, we create an R text variable in Displayr to store the new date as a string by:
- Either hover over any variable in your Data Sets tree > Plus (+) > Insert Variable(s) > Custom Code > R - Text or else from the toolbar menu, select Anything > Data > Variable > New > 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 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 Saving. - Finally, to this text variable back into a date variable, we simply need to go to the object inpsector and under Properties > GENERAL > Structure select Date/Time from the drop-down.
See Also
How to Work with Aggregated Dates Using R
Comments
0 comments
Article is closed for comments.