Date and Time in R Programming [With Easy Examples]

Filed Under: R Programming
DATE And TIME In R Programming

Hello, readers! Let us now focus on an interesting concept of R programming — Date and Time in R in detail.

So, let us begin!!

Today, we will be dealing with Date and Time in the current scenario i.e. in the current state of time.

R provides us various functions to get the date and time values from the system in different formats.

In this article, we will be covering certain functions that help us have the real-time date-time values.

Recommended read – Top 10 Best Packages in R


Type 1: R date() function

R provides us with date() function to extract the current date and time in R from the system and print it. It returns the date value along with the current timestamp in the standard format. The date() function takes no arguments.

Example:

#Removed all the existing objects
rm(list = ls())

print(date())

Solution:

"Fri Jan 29 20:51:22 2021"

Type 2: The Sys.Date() function

Apart from the date() function, R provides us with Sys.Date() function to get the current date value from the system. Unlike the date() function, the Sys.Date() function returns only the date value from the system and not the time values.

Syntax:

Sys.Date()

It does not accept any arguments and fetches the current date values to return.

Example:

#Removed all the existing objects
rm(list = ls())

print(Sys.Date())

Output:

"2021-01-29"

Type 3: The Sys.time() function

The Sys.time() function also returns the current time-stamp from the system in terms of date and time and the format like IST, CET, etc.

If you observe closely, you would get to know that the date() and Sys.time() function differs in terms of the format of their outputs.

Example:

#Removed all the existing objects
rm(list = ls())

print(Sys.time())

Solution:

"2021-01-29 20:56:38 IST"

Type 4: The Sys.timezone() function

R provides us with Sys.timezone() function to get the current time zone set through the system.

Syntax:

Sys.timezone()

Example:

#Removed all the existing objects
rm(list = ls())

print(Sys.timezone())

Output:

"Asia/Calcutta"

Type 5: The today() function

Now, R lubridate library provides us with today() function to get the current values from the system in terms of the date values only.

Example:

#install.packages('lubridate')
library(lubridate)
today()

Output:

"2021-01-30"

Type 6: The now() function

R provides us with now() function as well to fetch the current date and time values from the system.

The now() function resembles the functioning of Sys.time() function.

Example:

#install.packages('lubridate')
library(lubridate)
now()

Output:

"2021-01-30 08:04:15 IST"

Type 7: Get the components of the date-time

We can even extract the portions of the date-time values such as year, month, day, month-date, etc from the current time stamp using the below functions from the lubridate library:

  • year(): Extracts the year value from the current date-time values.
  • month(): Represents the month value from the current timestamp.

Example:

library(lubridate)
dte = today()
print(dte)

print(year(dte))
print(month(dte))

Output:

> print(dte)
[1] "2021-01-30"
> 
> print(year(dte))
[1] 2021
> print(month(dte))
[1] 1

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to R programming, Stay tuned!!

Till then, Happy Learning! 馃檪

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content