LoginSignup
34
22

More than 5 years have passed since last update.

date コマンドで日時のミリ秒単位まで表示する

Posted at

date コマンドの出力フォーマットに %N を指定するとナノ秒が表示できる。ミリ秒を表示するには上位3桁分だけ表示するように %3N を指定すればいい。

%N は GNU Coreutils の拡張なので GNU ではない date コマンドでは使えない。

GNU Coreutils: Time conversion specifiers

‘%N’
nanoseconds (‘000000000’…‘999999999’). This is a GNU extension.

例えば以下のシェルスクリプトでは「年月日_時分秒_ミリ秒」のフォーマットで日時を表示することができる。

#!/bin/sh
DATETIME=`date +%Y%m%d_%H%M%S_%3N`
echo $DATETIME

シェルスクリプトを何度か実行した例。

$ ./foobar.sh
20170726_123413_433
$ ./foobar.sh
20170726_123413_664
$ ./foobar.sh
20170726_123413_888
$ ./foobar.sh
20170726_123414_129
$ ./foobar.sh
20170726_123414_361
$ ./foobar.sh
20170726_123414_576
34
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
22