Get startedGet started for free

Setting the timezone

If you import a datetime and it has the wrong timezone, you can set it with force_tz(). Pass in the datetime as the first argument and the appropriate timezone to the tzone argument. Remember the timezone needs to be one from OlsonNames().

I wanted to watch New Zealand in the Women's World Cup Soccer games in 2015, but the times listed on the FIFA website were all in times local to the venues. In this exercise you'll help me set the timezones, then in the next exercise you'll help me figure out what time I needed to tune in to watch them.

This exercise is part of the course

Working with Dates and Times in R

View Course

Exercise instructions

I've put the times as listed on the FIFA website for games 2 and 3 in the group stage for New Zealand in your code.

  • Game 2 was played in Edmonton. Use force_tz() to set the timezone of game 2 to "America/Edmonton".
  • Game 3 was played in Winnipeg. Use force_tz() to set the timezone of game 3 to "America/Winnipeg".
  • Find out how long the team had to rest between the two games, by using as.period() on the interval between game2_local and game3_local.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Game2: CAN vs NZL in Edmonton
game2 <- mdy_hm("June 11 2015 19:00")

# Game3: CHN vs NZL in Winnipeg
game3 <- mdy_hm("June 15 2015 18:30")

# Set the timezone to "America/Edmonton"
game2_local <- ___(game2, tzone = ___)
game2_local

# Set the timezone to "America/Winnipeg"
game3_local <- ___(game3, tzone = ___)
game3_local

# How long does the team have to rest?
as.period(___)
Edit and Run Code