Home > Software design >  R current time in milliseconds
R current time in milliseconds

Time:05-15

How can I get the current time in milliseconds? I tried the below without success:

> strptime(Sys.time(), "%Y-%m-%d %H:%M:%OS")
[1] "2022-05-14 19:42:53 CEST

CodePudding user response:

You do get milliseconds by default on all operating systems, and (almost as the effective resolution is just a fraction less) microseconds on Linux and macOS -- but you must enable the printing of it.

Default R behaviour

> options(digits.secs=0) 
> Sys.time()             
[1] "2022-05-14 13:01:57 CDT" 
> 

Changed to Six Digits

> options(digits.secs=6)
> Sys.time()
[1] "2022-05-14 13:02:54.038276 CDT"
> 

I actually set this in my default ~/.Rprofile to always have six decimals.

CodePudding user response:

format(Sys.time(), "%Y-%m-%d %H:%M:%OS3")

#[1] "2022-05-14 11:01:17.928"
  •  Tags:  
  • r
  • Related