Convert datetimes to character timestamps in compact, ISO-like or human-readable styles, suitable for file names, identifiers, labels, and other reproducible text output.
timeStamp(datetime = NULL, timezone = NULL, unit = "sec", style = "ymdhms")Character vector of timestamps.
Input values are converted with parseDatetime() using the required
timezone argument. When datetime = NULL, the current UTC time is used
and timezone defaults to "UTC".
The unit argument controls the precision of the output timestamp. The
style argument controls the output format.
Supported unit values are:
"year"
"month"
"day"
"hour"
"min"
"sec"
"msec"Supported style values are:
"ymdhms" compact calendar time
"ymdThms" compact calendar time with "T" separator
"julian" year and Julian day
"clock" ISO-like clock time
"human" clock time with a space instead of the "T" separator
For style = "human", hour timestamps always include minutes (:00), e.g.
"2019-01-08 12:00", so that every timestamp containing a time of day looks
like a time of day.
For style = "julian" and unit = "month", the timestamp uses the Julian
day associated with the beginning of the month.
When startdate or enddate are already POSIXct values, they are first
converted to timezone with lubridate::with_tz() without changing the
represented instant in time.
datetime <- parseDatetime("2019-01-08 12:30:15", timezone = "UTC")
timeStamp()
#> [1] "20261012154327"
timeStamp(datetime, "UTC", unit = "year")
#> [1] "2019"
timeStamp(datetime, "UTC", unit = "month")
#> [1] "201901"
timeStamp(datetime, "UTC", unit = "month", style = "julian")
#> [1] "2019001"
timeStamp(datetime, "UTC", unit = "day")
#> [1] "20190108"
timeStamp(datetime, "UTC", unit = "day", style = "julian")
#> [1] "2019008"
timeStamp(datetime, "UTC", unit = "hour")
#> [1] "2019010812"
timeStamp(datetime, "UTC", unit = "min")
#> [1] "201901081230"
timeStamp(datetime, "UTC", unit = "sec")
#> [1] "20190108123015"
timeStamp(datetime, "UTC", unit = "sec", style = "ymdThms")
#> [1] "20190108T123015"
timeStamp(datetime, "UTC", unit = "sec", style = "julian")
#> [1] "2019008123015"
timeStamp(datetime, "UTC", unit = "sec", style = "clock")
#> [1] "2019-01-08T12:30:15"
timeStamp(datetime, "America/Los_Angeles", unit = "sec", style = "clock")
#> [1] "2019-01-08T04:30:15"
timeStamp(datetime, "America/Los_Angeles", unit = "msec", style = "clock")
#> [1] "2019-01-08T04:30:15.000"
timeStamp(datetime, "UTC", unit = "hour", style = "human")
#> [1] "2019-01-08 12:00"
timeStamp(datetime, "UTC", unit = "min", style = "human")
#> [1] "2019-01-08 12:30"
timeStamp(datetime, "UTC", unit = "sec", style = "human")
#> [1] "2019-01-08 12:30:15"