Inbuilt Functionalities

Breaks on date axes in years:

scale_x_date(date_breaks = "5 years", date_labels = "%Y")

Types of Datetimes

  1. POSIXct : seconds since 1970-01-01
  2. POSIXlt : list with named components
as.POSIXct("date-string")

UTC as Timezone in Datetime object: 2010-01-01 00:00:00Z will mean a UTC timezone object and will be parsed accordingly. Else the time is local. You can change the timezone in POSIXct object though.

as.POSIXct("date-string", tz= "UTC")

This line preserves the datetime in UTC.

Lubridate Package

ID: d86831a7-85c0-426e-a185-cdd1debe8613

Basic Parsing

ymd("year_month_date_string")
dmy("similar_string")

Other similar functions are mdy, myd, dmy, dym etc. Now for working with time alongside dates we have similar functions.

dmy_hm("date_month_year_string hh:mm")
parse_date_time(x = "sting_to_parse", order = "pattern_to_parse_with")
# e.g. order = "dmy"

Parsing object with no basic fields

parse_date_time("string", orders = "d0mY", "0mY", "Y")

Here the argument second has no year to parse, third one has just the year to be parsed.

  • What happens to a missing field?

    The missing fields are just set to “1”.

Making a new date object

make_date(year= 2011, month = 1, day=18)

Extracting individual elements

year()
month()
day()
wday() # weekday 1-7
wday(label = TRUE) # gives those days as Sun, Mon
yday() # day of year 1-366
min()
second()

Setting individual parts only

year(date_obj) <- 2011x

More information extraction from dates

leap_year()
am()
pm()
dst() # daylight savings
quarter()
semester()

Rounding datetimes

floor_date(date_obj, unit = "hour")
round_date(date_obj, unit = "hour")
ceiling_date(date_obj, unit = "hour")

Units can be second, minute, hour, day , bimonth, quarter, halfyear or year. You can use multiples.

floor_date(date_obj, unit = "2 hours")

Differences of datetimes

difftime(date1, date2)
difftime(date1, date2, units = "weeks")
date1 - date2
now()
today()

Both of the commands work in a similar fashion.

Time Spans

  1. Period: Human concept of time span e.g. You have a datetime and adding 1 day means you get the same time on the day added by 1.
  2. Duration: Fixed length of time in seconds e.g. It’s actually 86400 seconds.
days() # creates period of 1 day
days(x = 2) # period
ddays(2) # 2 days duration

“d” is appended to every period function to behave it as duration function except months for which we don’t have a duration function. It’s because months are irregular.

  • Sequence of datetimes

    1:10 * months(1)
    1:26 * weeks(2)
    
  • Adding and Subtracting timespans

    jan31 = "2019-01-31"
    month_seq <- 1:12 * months(1)
    jan31 + month_seq
    

    This will result in undefined values across different months which don’t have dates upto 31 in that month. Now, to get with that issue:

    jan31 %m+% month_seq
    jan31 %m-% month_seq
    

Intervals

date_1 %--% date_2 # create an interval
interval(date_1, date_2) # create interval obj similar
as.period(intervalObj)
as.duration(intervalObj)
date %within% intervalObj
int_overlaps(interval1, interval2)
  • When to use intervals?

    When you have start and end

Time Zones

  • Getting your timezone

    Sys.timezone()
    
  • Listing timezones

    OlsonNames()
    
  • Setting and Extracting Timezone

    date_obj <- date_obj + tz= "tz_string"
    
  • Manipulating timezones

    • Force a timezone without changing clock time

      force_tz(date_obj, tzone = "timezone_name")
      
    • View same time in different timezone

      with_tz(date_obj, tzone = "timezone_string")
      
    • Fast Parsing of datetimes

      parse_date_time() # slow implementation
      fast_strptime(x, format = "%Y-%m-%d")
      library(fasttime)
      fastPOSIXct("date_string")
      
    • Stamp Functionalities

      Stamp() takes a string which should be an example of how the date should be formatted, and returns a function that can be used to format dates.

      **