20

I want this command in windows with cygwin installed, so any Linux command will work for me.

1

4 Answers 4

16

date +%H:%M:%S:%N will give you the current time with nano seconds, you could then chop off however many digits or rearrange the time to how you wish to have it.

date --help can give you some other configuration options

1
  • 1
    Maybe it should be noted that on most platforms, you don't get exact nanoseconds. For instance in my case, the last two digits are always zero, from which I conclude that i get at best a resolution of 100ns precision. Jul 14, 2022 at 7:41
43

If you want to get milliseconds instead of nanoseconds, 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)«

Also tested successfully in my cygwin installation.

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
4
  • Since date is not a shell builtin, date --version or similar are relevant, not the exact shell version.
    – Daniel Beck
    Apr 12, 2014 at 17:24
  • 1
    You're right. In this case, it was busybox v1.21.0 (buildroot rootfs, compiled using a non-HF arm cross toolchain). date --version gave unrecognized option error there.
    – Joe
    Apr 12, 2014 at 23:38
  • 1
    4 years later and I gotta say THANK YOU I just learned that you can truncate from the date command itself. Amazing.
    – emmdee
    Aug 23, 2018 at 23:18
  • 2021 - and the %N is not working in WSL. export PS1="[\033[33m]\D{%H:%M:%S.%N}[\033[00m] [\033[32m]\u@$Ubuntu-20.04[\033[00m]:[\033[36m]\w[\033 [00m]$ " Gets me: 10:16:25.%N [email protected]:/mnt/c/WINDOWS/system32$ Sep 19, 2021 at 17:17
2

Here is the command where you can print the json-like ISO format current time:

date +%FT%T.%3N
1

To be ISO complete I would use either:

date +%FT%T.%3N%z

for lcal time, or:

date -u +%FT%T.%3NZ

for UTC.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .