POSIXlt and POSIXct are the two built-in data types for date-times in R. This Q&A has more explanation.
Under the hood, both describe the amount of time that has passed since the first moment of 1970.
Here I define two POSIXlt values. I am in PST time zone, 8 hours behind GMT, so the first date-time I picked is actually the same moment as 1970-01-01 00:00 GMT
my_times <- as.POSIXlt(c("1969-12-31 16:00", "2021-04-15 13:49"))
my_times
#[1] "1969-12-31 16:00:00 PST" "2021-04-15 13:49:00 PDT"
Under the hood, each timestamp in POSIXlt format is a list of numbers each describing the year, date, hour, etc., with some extra flags to tell R that its a POSIXlt, my timezone and whether its daylight savings time, etc.
# dput creates code that would reproduce those values. You'll see that
# it encodes them by coding the year, month, day, hour, etc.
dput(my_times)
#structure(list(sec = c(0, 0), min = c(0L, 49L), hour = c(16L,
#13L), mday = c(31L, 15L), mon = c(11L, 3L), year = c(69L, 121L
#), wday = 3:4, yday = c(364L, 104L), isdst = 0:1, zone = c("PST",
#"PDT"), gmtoff = c(NA_integer_, NA_integer_)), class = c("POSIXlt",
#"POSIXt"))
# FYI, POSIXct stores the numbers directly as seconds since 1970
# (equivalent to the way POSIXct stores them)
structure(c(0, 1618519740), class = c("POSIXct", "POSIXt"), tzone = "")
If we need to convert those two timestamps each to a single number, R will convert it to the number of seconds since the start of 1970. The first one was picked to be that moment (0 seconds elapsed), while about 1.6 billion seconds have now elapsed.
as.numeric(my_times)
#[1] 0 1618519740
# approx years since start of 1970
# calculated by looking at the difference between the two numbers
diff(as.numeric(my_times))/(24*60*60*365.25)
#[1] 51.2878
To convert to character, you can use as.character
to get them like this:
as.character(my_times)
#[1] "1969-12-31 16:00:00" "2021-04-15 13:49:00"
If you need them in a specific time format, see here for a review of those using the strptime
function: