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 more 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:

 

The YouTube video will be added soon.

 

In addition, you might read the other posts on my website.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top