Convenience function to generate a nicely formatted character string with the date and time, typically of the form: YYYY_MM_DD-HH_MM to include as part of a file name. Can convert the character string back into a date and time if needed.

date_and_time(value = NULL, frmt = "%Y_%m_%d-%H_%M")

Arguments

value

A character string to convert back into a date-time object.

frmt

A character string specifying the format for the date and time object (see as.POSIXct).

Value

Either 1) a character string with the date and time, to include in a file name, or 2) a date-time object.

Examples

string <- date_and_time()
string
#> [1] "2024_10_19-11_25"
# Convert back to date and time object
format( date_and_time( string ), '%Y-%m-%d %H:%M' )
#> [1] "2024-10-19 11:25"