'R current time in milliseconds

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
r


Solution 1:[1]

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.

Solution 2:[2]

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

#[1] "2022-05-14 11:01:17.928"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Jon Spring