These functions take a POSIXct or a numeric object and convert to a date-time string with the format specified by the function. Years are always 4 digits long with all other parts of the string 2 digits long with leading zeros where necessary. In the case of numeric input, the number is assumed to be in seconds since 1970-01-01 00:00:00 in UTC.
Usage
as_str_dttm(x)
as_YMD(x)
as_ymd(x)
as_YMDh(x)
as_ymdh(x)
as_YMDhm(x)
as_ymdhm(x)
as_YMDhms(x)
as_ymdhms(x)
Details
as_str_dttm
returns a string with trailing zeros truncated. The
other functions are named such that the output is truncated to the precision
given by the function name. Note that no rounding is done, only truncation.
There are all lower case versions of functions as well as those with Y, M, D
in upper case.
Examples
as_str_dttm(as.POSIXct("2022-03-08 00:00:00"))
#> [1] "20220308"
as_str_dttm(as.POSIXct("2022-03-08 10:00:00"))
#> [1] "2022030810"
as_str_dttm(as.POSIXct("2022-03-08 10:30:00"))
#> [1] "202203081030"
as_str_dttm(as.POSIXct("2022-03-08 10:30:20"))
#> [1] "20220308103020"
as_YMD(Sys.time())
#> [1] "20240202"
as_YMDh(Sys.time())
#> [1] "2024020215"
as_YMDhm(Sys.time())
#> [1] "202402021547"
as_YMDhms(Sys.time())
#> [1] "20240202154729"
as_YMD(as.numeric(Sys.time()))
#> [1] "20240202"
as_YMDh(as.numeric(Sys.time()))
#> [1] "2024020214"
as_YMDhm(as.numeric(Sys.time()))
#> [1] "202402021447"
as_YMDhms(as.numeric(Sys.time()))
#> [1] "20240202144729"