Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to correctly use as.POSIXct function inside a cat function

Tags:

r

posixct

I would like to get a data format like "yyyy-mm-dd hh:mm:ss timezone" for Sys.time() as part of the "cat" function. When I use the two functions by themselves there is no problem but when I use the conversion function inside the cat function I don't get the desired data format. How can I solve this issue?

> cat("THE TIME NOW: ",as.POSIXct(Sys.time(), origin="1970-01-01"))
THE TIME NOW:  1468251677
> as.POSIXct(Sys.time(), origin="1970-01-01")
[1] "2016-07-11 18:41:21 IDT"
like image 877
mql4beginner Avatar asked Jul 02 '26 00:07

mql4beginner


1 Answers

I guess this is what you want:

cat("THE TIME NOW: ",as.character(as.POSIXct(Sys.time(), origin="1970-01-01"),usetz=T),sep="")

Output:

THE TIME NOW: 2016-07-11 16:55:15 BST

I just had to transform the output of the function as.POSIXct(Sys.time()) to character before using cat. The parameter usetz=T tells the as.character function that you want to keep the timezone.

like image 123
Diego Rodrigues Avatar answered Jul 05 '26 15:07

Diego Rodrigues