Get Formatted Date From Timestamp With Rounded Milliseconds Bash Shell Script
You may simply use %3N
to truncate the nanoseconds to the 3 most significant digits:
$ date +"%Y-%m-%d %H:%M:%S,%3N"
2014-01-08 16:00:12,746
or
$ date +"%F %T,%3N"
2014-01-08 16:00:12,746
testet with »GNU bash, Version 4.2.25(1)-release (i686-pc-linux-gnu)«
But be aware, that %N may not implemented depending on your target system or bash version.
Tested on an embedded system »GNU bash, version 4.2.37(2)-release (arm-buildroot-linux-gnueabi)« there was no %N
:
date +"%F %T,%N"
2014-01-08 16:44:47,%N
Million ways to do this.. one simple way is to remove the trailing numbers...
date +'%Y-%m-%d %H:%M:%S.%N' | sed 's/[0-9][0-9][0-9][0-9][0-9][0-9]$//g'
or
date +'%Y-%m-%d %H:%M:%S.%N' | sed 's/......$//g'
I can't help offering a "cheap" solution...
echo $(date +"%Y-%m-%d-%H:%M:%S.")$((RANDOM%1000))
After all, if accuracy is less important than uniqueness, as it is in my case... here you have it.