iskandarblue
iskandarblue

Reputation: 7526

formatting date and timezone in R

In my Rmarkdown document, I would like to display the date and the timezone in the header while omitting the actual time.

At the moment, the current code displays the date but leaves out the timezone:

date: "`r format(Sys.Date(), '%d %B, %Y')`" 

> format(Sys.Date(), '%d %B, %Y')
[1] "19 March, 2018"

> Sys.time()
[1] "2018-03-19 22:22:11 EDT"

However, if I use strsplit() on Sys.time(), the time zone is omitted:

   > strsplit(as.character(Sys.time()), " ")
[[1]]
[1] "2018-03-19" "22:25:05" 

Equally, this solution works in the console but throws and error in Rmarkdown:

paste(format(Sys.Date(), '%d %B, %Y'), tz(Sys.Date()))

Warning: Error in tz: could not find function "tz"

What would be the proper way to format Sys.time() or Sys.date() in Rmarkdown for the display to only appear as such:

"2018-03-19 EDT"

Upvotes: 0

Views: 1985

Answers (1)

Chris Holbrook
Chris Holbrook

Reputation: 2636

Use format with timezone specification (%Z or %z)

format(Sys.time(), "%Y-%m-%d %Z")

Upvotes: 4

Related Questions