The date and time objects in R programming language can be represented using character strings in R. The date and time objects can be merged together using POSIX format or in the form of datetime objects. The POSIXlt class stores date and time information.
Discussed below are various approaches to combine date and time in R.
Method 1 : Using M3 package
The M3 package in R language. The combine.data.and.time() method in R can be used to merge together the date and time to obtain date-time object in POSIX format.
Syntax: combine.date.and.time(date, time)
Parameter :
- date – Date can be specified either in the Date format or in the form of character string referred by “YYYY-MM-DD”.
- time – Time can be specified either in the form of a list consisting of hrs , mins and secs elements or in the form of character string referred by HH:MM:SS (with hours ranging from 00-23).
Returns :
Returns the date-time combined in the POSIX format.
The returned date-time object follows the GMT time zone by default. In case, the date or time is not a valid date or time object, NA is returned as the output.
Example 1:
R
library ( "M3" )
date_obj <- "2021-06-04"
time_obj <- "23:02:34"
combine.date.and.time (date = date_obj, time = time_obj)
|
Output
[1] “2021-06-04 23:02:34 GMT”
The date can also be specified using the R Date object to perform the date and time combine operation.
Example 2:
R
library ( "M3" )
date_obj <- "2021-06-04"
date_obj <- as.Date (date_obj)
time_obj <- "23:02:34"
combine.date.and.time (date = date_obj, time = time_obj)
|
Output
[1] “2021-06-04 23:02:34 GMT”
The time can also be specified using the R list consisting of hours, minutes, and seconds elements of the object to perform date and time combine operation.
Example 3:
R
library ( "M3" )
date_obj <- "2021-06-04"
date_obj <- as.Date (date_obj)
time_obj <- list (hrs=22, mins=08, secs=35)
combine.date.and.time (date = date_obj, time = time_obj)
|
Output
[1] “2021-06-04 22:08:35 GMT”
Method 2 : as.POSIXct method
The date and time strings can be converted together to form a string using the paste() method. The POSIXct method comprised of functions to work and manipulate objects of classes belonging to “POSIXlt” and “POSIXct” representing calendar dates and times. The specified date-time object is converted to the specified string format.
Syntax:
Syntax:
as.POSIXct(date-time, tz, format=”%Y-%m-%d %H:%M:%S”)
Parameter :
- date-time : The string date-time object to be converted to the specified format.
- tz : The time zone to convert the object into. Default is the UTC time zone.
Returns :
Returns the date-time combined in the POSIXct format.
Example:
R
date_obj <- "2021-06-04"
date_obj <- as.Date (date_obj)
time_obj <- "22:08:35"
format <- "%Y-%m-%d %H:%M:%S"
as.POSIXct ( paste (date_obj, time_obj), format=format)
|
Output
[1] “2021-06-04 22:08:35 IST”
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
11 Aug, 2021
Like Article
Save Article