stevec
stevec

Reputation: 52228

Convert Sys.time() to character without losing timezone?

How can we convert the output of Sys.time() to character class without losing the timezone information (in this case the AEDT) ?

Sys.time()
# [1] "2019-10-17 21:54:07 AEDT"

Sys.time() %>% as.character()
# [1] "2019-10-17 21:54:07"

# Desired output
"2019-10-17 21:54:07 AEDT"

Please note that I wish to avoid using any external libraries (just base R)

Upvotes: 3

Views: 122

Answers (1)

deepseefan
deepseefan

Reputation: 3791

This gives you a character

x <- format(Sys.time(), usetz = TRUE)
str(x)
# chr "2019-10-17 22:13:45 AEDT"

usetz is FALSE by default, set that to TRUE so it persists during the conversion.

Upvotes: 4

Related Questions