Get timestamp including milliseconds
DateTime#strftime has a %L
format specifier for milliseconds:
$ ruby -e "puts Time.now.strftime('%Y%m%d%H%M%S%L')"
20130625141141827
Update: To answer the question in your comment, yes, this is possible. Clicking on the documentation link above, there's %N
for fractional second digits. To remove the last digit from above, use %2N
rather than %L
(%3N
is equivalent to %L
). To get just deciseconds, use %1N
.
Related videos on Youtube
Peter Burns
Stack Overflow Valued Associate #00001 Wondering how our software development process works? Take a look! Find me on twitter, or read my blog. Don't say I didn't warn you because I totally did. However, I no longer work at Stack Exchange, Inc. I'll miss you all. Well, some of you, anyway. :)
Updated on September 14, 2022Comments
-
Peter Burns about 1 year
I use this command a lot on OS X to create a timestamp for archiving purposes:
date -n +%Y%m%d%H%M%S
This gives an answer in this format:
20130625230005
I'd like to add milliseconds to the end of this string. Is it possible to get that with Ruby from the command line?
It's unfortunately not possible to do this on OS X by adding something to the command above: Add milliseconds to timestamp (bash, unix)