Change Default Time Zone in R (2 Examples)
In this post, you’ll learn how to modify the default system time zone in R.
The content of the article is structured as follows:
Let’s jump right to the R code.
Example 1: Get Current System Time & Zone
Example 1 explains how to return the currently used system time and time zone in R.
To get the current time, we can use the Sys.time function as shown below:
Sys.time() # Get current system time # [1] "2021-08-26 09:56:50 CEST"
The time when creating this tutorial is 2021-08-26 09:56:50 CEST.
We can return only the time zone by using the Sys.timezone function:
Sys.timezone() # Get current system time zone # [1] "Europe/Berlin"
As you can see. our system time correspond to Europe/Berlin.
Let’s change these times to a different time zone!
Example 2: Change System Time & Zone Using Sys.setenv() Function
In this example, I’ll demonstrate how to switch from one time zone to another.
For this task, we can apply the Sys.setenv as shown in the following R code:
Sys.setenv(TZ = "GMT") # Change time zone
Let’s run the Sys.time…
Sys.time() # Get new system time # [1] "2021-08-26 07:56:50 GMT"
…and the Sys.timezone functions once again:
Sys.timezone() # Get new system time zone # [1] "GMT"
As you can see, we have changed our system time to GMT. Depending on your preferences, you may replace GMT by other time zones such as UTC, NZ, GB, or EST.
You may find a full list of available time zones by executing the OlsonNames() function in your RStudio console.
Video & Further Resources
Do you need further information on the R codes of this tutorial? Then you might want to watch the following video on my YouTube channel. In the video, I’m explaining the topics of this tutorial in RStudio:
Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.
If you accept this notice, your choice will be saved and the page will refresh.
In addition, you might read the other posts on my website.
- Introduction to System Calls & Commands
- R.Version Function in R
- Change Default Working Directory in R & RStudio (Step-by-Step Example)
- strptime & strftime in R
- R Programming Overview
In this tutorial, you have learned how to change the default time zone in the R programming language. In case you have additional questions, please let me know in the comments section.
Statistics Globe Newsletter