# Dates and times ## Introduction This chapter will show you how to work with dates and times in R. At first glance, dates and times seem simple. You use them all the time in every day, and generally have too many problems. However, the more you learn about dates and times, the more complicated the get. For example: * Does every year have 365 days? * Does every day have 24 hours? * Does every minute have 60 seconds? I'm sure you remembered that there are leap years that have 365 days (but do you know the full rule for determining if a year is a leap year?). You might have remembered that many parts of the world use daylight savings time, so that some days have 23 hours, and others have 25. You probably didn't know that some minutes have 61 seconds because occassionally leap seconds are added to keep things in synch. Read for even more things that you probably believe that are not true. Dates and times are hard because they have to reconcile two physical phenonmen (the rotation of the Earth and its orbit around the sun) with a whole raft of cultural phenonmeon including months and time zones. This chapter won't teach you everything about dates and times, but it will give you a solid grounding of practical skills that will help you with common data analysis challenges. ### Prerequisites This chapter will focus on the __lubridate__ package, which makes it easier to work with dates and times in R. We will use nycflights13 for practice data, and some packages for EDA. ```{r setup, message = FALSE} library(lubridate) library(nycflights13) library(dplyr) library(ggplot2) ``` ## Creating date/times There are three important * A __date__. Number of days since Jan 1, 1970. `` * A __date-time__ is a date plus a time. POSIXct. (We'll come back to POSIXlt later - but generally you should avoid it.). Number of seconds since Jan 1, 1970. `` * A __time__, the number of seconds. A date + a time = a date-time. Not discussed furher in this chapter. `