Tutorial by Examples

The lubridate package provides convenient functions to format date and datetime objects from character strings. The functions are permutations of LetterElement to parseBase R equivalentyyear%y, %Ym (with y and d)month%m, %b, %h, %Bdday%d, %ehhour%H, %I%pm (with h and s)minute%Msseconds%S e.g. ymd(...
Lubridate provides ymd() series of functions for parsing character strings into dates. The letters y, m, and d correspond to the year, month, and day elements of a date-time. mdy("07-21-2016") # Returns Date ## [1] "2016-07-21" mdy("07-21-2016", t...
date <- now() date ## "2016-07-22 03:42:35 IST" year(date) ## 2016 minute(date) ## 42 wday(date, label = T, abbr = T) # [1] Fri # Levels: Sun < Mon < Tues < Wed < Thurs < Fri < Sat day(date) <- 31 ## "2016-07-31 03:42:35 IST" # If an ...
An instant is a specific moment in time. Any date-time object that refers to a moment of time is recognized as an instant. To test if an object is an instant, use is.instant. library(lubridate) today_start <- dmy_hms("22.07.2016 12:00:00", tz = "IST") # default tz="UT...
Intervals are simplest way of recording timespans in lubridate. An interval is a span of time that occurs between two specific instants. # create interval by substracting two instants today_start <- ymd_hms("2016-07-22 12-00-00", tz="IST") today_start ## [1] "2016-07-...
now_dt <- ymd_hms(now(), tz="IST") now_dt ## [1] "2016-07-22 13:53:09 IST" round_date() takes a date-time object and rounds it to the nearest integer value of the specified time unit. round_date(now_dt, "minute") ## [1] "2016-07-22 13:53:00 IST" r...
Unlike durations, periods can be used to accurately model clock times without knowing when events such as leap seconds, leap days, and DST changes occur. start_2012 <- ymd_hms("2012-01-01 12:00:00") ## [1] "2012-01-01 12:00:00 UTC" # period() considers leap year calculati...
with_tz returns a date-time as it would appear in a different time zone. nyc_time <- now("America/New_York") nyc_time ## [1] "2016-07-22 05:49:08 EDT" # corresponding Europe/Moscow time with_tz(nyc_time, tzone = "Europe/Moscow") ## [1] "2016-07-22 12:49:...

Page 1 of 1