Working with Time  and Date variables in R Programming Language


There are a variety of different types specific to time data fields in R. Here we only look at two, the POSIXct and POSIXlt data types:


The POSIXct data type shows the number of seconds since the start of January 1 , 1970. If the date is before January 1, 1970 than it shows in negative numbers.


Contact at , if you are looking for an Instructor Based Online Training !

The POSIXlt data type is a vector  and it shows following values:

1. Seconds

2. Minutes

3. Hours

4.Day of the month(1-31)

5. Month of the year(0-11)

6. Years since 1900

7. Day of the week(0-6 where 0 represents Sunday)

8. Day of the year(0-365)

9. Daylight savings time indicator  

We first check the various parameters of POSIXct and POSIXlt by taking help()  


It will open Help window to show DateTimeClasses.


We can check today's date by using Sys.Date() function.

today <- Sys.Date()


It shows date in YYYY-MM-DD format.

We have following code values for date and time to show:

Code Value

%d    Day of the month (decimal number)

%m    Month (decimal number)

%b    Month (abbreviated)

%B    Month (full name)

%y    Year (2 digit)

%Y    Year (4 digit)


We can use above code to show date in different formats.

format(today, format = "%B %d %Y")

We can change any data type to Date format by using as.Date() function . Here , we change character data type to date data type .

x <- as.Date("1970/01/01")  


We check data type of "x" object .


We can also specify format to read data in given format .

as.Date("1/15/2001", format = "%m/%d/%Y")

as.Date("April 26,2001", format= "%B %d, %Y")

as.Date("22JUN01", format= "%d%b%y")

We create an object "Birth" to store date value. We store date under names of person.

Birth <- c(lara =as.Date("1980-06-14"), katy = as.Date("1987-09-19"), Priya = as.Date("1990-01-22"))

We can find month and weekday of date by using months() and weekdays() function respectively.



We can find date differences between two days as :

y <- Sys.Date() - as.Date("1980-06-14")


It shows date difference between current date and date specified.

We can also used difftime() function to find difference between dates .

We take help to know more about difftime() function.

x1 <- as.Date("1980-06-14")

y1 <- Sys.Date()


We can find week difference between two dates .

difftime(y1, x1,units = "weeks")

We check current time as :

z <- Sys.time()

We create an object of POSIXlt data type as:

p <- as.POSIXlt(z)


It shows all nine parameters as seconds , minutes , hours , day of month , month of the year , years since 1990 , day of the week ,day of the year  and daylight saving time. It includes time zone as "IST" .

It also shows gmtoff parameter which is the offset in seconds from GMT time zone.

We want to check all the parameters of POSIXlt data type.


We can also extract specific parameter of POSIXlt object .

We extract hour and day of the year parameter as:



We create an object of POSIXct type. It only show seconds time from January 1 , 1970.

p <- as.POSIXct(z)


We can also see time from various time zone as:

as.POSIXlt(Sys.time(), "GMT")

To easily work with dates , we use lubridate package . We install and load lubridate package as:



We are using interval() function to make interval between two dates.

span <- interval(as.POSIXct("2009-01-01"), as.POSIXct("2010-02-02 01:01:01"))

We find difference between two dates by using as.period() :


It shows difference between two dates as one year one month one day one hour one minute and one second.

We find difference between today and date specified.

abc <- interval( as.Date("1980-06-14"), Sys.Date())


We can also find days difference between two dates by using unit parameter in as.period(). By specifying unit = "days" to find difference as days difference.

leap <- interval(ymd("2016-01-01"), ymd("2017-01-01"))

as.period(leap, unit = "days")

It shows 366 days between two dates.

We can also find years difference between dates.

as.period(leap, unit = "years")

We use ymd() to read dates as yyyy-mm-dd and time zone(tz) as "America/Chicago".

dst <- interval(ymd("2016-11-06", tz = "America/Chicago"),

               ymd("2016-11-07", tz = "America/Chicago"))

It shows seconds difference between two dates.

strptime ()

To convert character type to POSIXt type.


We create a character vector as datestring.

datestring <- c("January 10,2012 10:40", "December 9, 2011 9:10")

We convert character type to POSIXt type and also provide format to read date as given format.

x <- strptime(datestring, "%B %d, %Y %H:%M")





We are using mdy_hm() to convert character to POSIXt type. We read as month , day , year , hour:minute  and store it in default date type as year , month and date.    

We create start and end object to store time.

start <- mdy_hm("3-11-2017 6:25", tz = "US/Eastern")

end <- mdy_hm("3-13-2017 6:25", tz = "US/Eastern")



You can notice how the time zone changes from EST to EDT indicating that Daylight Savings has started. 

We create an interval by using %--% operator.

time.interval <- start %--% end

To create a Duration between these two dates, we can use the as.duration() function.

time.duration <- as.duration(time.interval)

A duration object prints the elapsed time in seconds as well as something in days. It represent 1.96 days equals to 47 hours . The Daylight Savings went into effect at 2:00 AM during the interval , an hour was skipped . Thus the duration is only 47 hours.

Contact at , if you are looking for an Instructor Based Online Training !



There are no entries yet.
Please enter the code
* Required fields

Subscribe to Our YouTube Channel To recieve updates on Free Learning



Visit Our Website to View the Upcoming Training Schedule

Looking for Corporate Training ? Reach out to us at

Reach out to us if you are looking for Corporate Training to Build The Next Generation Analytical Workforce with an in-depth understanding of  Exploratory Data Analysis , Data Visualisation, Data Analytics , AI First , Machine Learning & Deep Learning Training & Consulting helping them to take Data Informed Decision at each stage of the business. 

We understand that At the present times , the Entire Industry is in a Tranformation stage with the Softwares  being rebuilt with Artificial Intelligence Capabilities .

We need SMART WORKFORCE for the SMART SOFTWARES to reap the maximum return . 


Whatsapp at +91-9953805788 or email at - if you would like to know more . 


  • Corporate Tableau Training in Gurgaon
  • Corporate Data Analytics Training in Gurgaon
  • Corporate Microsoft Power BI Training in Gurgaon
  • Corporate Microstrategy Training in Gurgaon
  • Corporate Google Data Studio Training in Gurgaon
  • Corporate Python Training in Gurgaon
  • Corporate Advance Analytics in R Programming Training in Gurgaon
  • Corporate Machine Learning Training in Gurgaon
  • Corporate Deep Learning Training in Gurgaon
  • Corporate Data Visualization Training in Gurgaon



Instrovate Technologies

Noida, Gurgaon 

Hit Your Refresh Button To Rise Higher

Subscribe to Our YouTube Channel To Receive Updates on the Latest Free Learning Video Added

Print Print | Sitemap
All rights reserved @ Instrovate Technologies

Terms Of Services    Privacy Policy    Disclaimer     Refund Policy