Open In App

How to merge date and time in R?

Last Updated : 11 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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")
 
# declaring date
date_obj <- "2021-06-04"
 
# declaring time
time_obj <- "23:02:34"
 
# combining date and time into
# single object
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")
 
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
 
# declaring time
time_obj <- "23:02:34"
 
# combining date and time into
# single object
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")
 
# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
 
# declaring time
time_obj <- list(hrs=22, mins=08, secs=35)
 
# combining date and time into
# single object
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




# declaring date
date_obj <- "2021-06-04"
date_obj <- as.Date(date_obj)
 
# declaring time
time_obj <- "22:08:35"
 
# specifying the format
format <- "%Y-%m-%d %H:%M:%S"
 
# combining date and time into single object
as.POSIXct(paste(date_obj, time_obj), format=format)


Output

[1] “2021-06-04 22:08:35 IST”



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads