Use functions from lubridate package

Here, By using this module, we can get the day, month, year, hour, minute, and second separately in integer format.

Syntax:

day:
day(date)

month:
month(date)

year:
year(date)

hour:
hour(date)

minute:
minute(date)

second:
second(date)

Example:

R




# load the library
library("lubridate")
  
# create date
data = as.POSIXct("1/1/2021  1:05:00 AM"
                  format="%m/%d/%Y  %H:%M:%S %p")
  
# display
print(data)
  
# get the day
print(day(data))
  
# get the month
print(month(data))
  
# get the year
print(year(data))
  
# get the hour
print(hour(data))
  
# get the minute
print(minute(data))
  
# get the second
print(second(data))


Output:

[1] "2021-01-01 01:05:00 UTC"
[1] 1
[1] 1
[1] 2021
[1] 1
[1] 5
[1] 0


How to Convert Date to Numeric in R?

In this article, we will discuss how to convert date to numeric in R Programming Language.

Similar Reads

Method 1: Using as.numeric()

This function is used to convert date into numeric...

Method 2: Use functions from lubridate package

...