Viewing in a timezone
To view a datetime in another timezone use with_tz(). The syntax of with_tz() is the same as force_tz(), passing a datetime and set the tzone argument to the desired timezone. Unlike force_tz(), with_tz() isn't changing the underlying moment of time, just how it is displayed.
For example, the difference between now() displayed in the "America/Los_Angeles" timezone and "Pacific/Auckland" timezone is 0:
now <- now()
with_tz(now, "America/Los_Angeles") -
with_tz(now, "Pacific/Auckland")
Help me figure out when to tune into the games from the previous exercise.
This exercise is part of the course
Working with Dates and Times in R
Exercise instructions
- Most fans will tune in from New Zealand. Use
with_tz()to displaygame2_localin New Zealand time. New Zealand is in the"Pacific/Auckland"timezone. - I'll be in Corvallis, Oregon. Use
with_tz()to displaygame2_localmy time. Corvallis is in the"America/Los_Angeles"timezone. - Finally, use
with_tz()to displaygame3_localin New Zealand time.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# What time is game2_local in NZ?
___(game2_local, tzone = ___)
# What time is game2_local in Corvallis, Oregon?
___(game2_local, tzone = ___)
# What time is game3_local in NZ?
___