Most common Bash date commands for timestamping

From time to time I get asked how to use the date command to generate a timestamp. Here is an idiot-friendly script you can post for reference in your team’s bin/ if you get interrupted about timestamp questions or have an aversion to typing phrases like “man date” (with or without a space).

All but the first and last two produce filename-friendly strings. (Thanks to Rich for the reminder to include UTC and timezoned stamps here):

#! /bin/bash

# An overly obvious reference for most commonly requested bash timestamps
# Now all you Mac fags can stop pestering me.

cat << EOD
        Format/result         |       Command              |          Output
------------------------------+----------------------------+------------------------------
YY-MM-DD_hh:mm:ss             | date +%F_%T                | $(date +%F_%T)
YYMMDD_hhmmss                 | date +%Y%m%d_%H%M%S        | $(date +%Y%m%d_%H%M%S)
YYMMDD_hhmmss (UTC version)   | date --utc +%Y%m%d_%H%M%SZ | $(date --utc +%Y%m%d_%H%M%SZ)
YYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z      | $(date +%Y%m%d_%H%M%S%Z)
YYMMSShhmmss                  | date +%Y%m%d%H%M%S         | $(date +%Y%m%d%H%M%S)
YYMMSShhmmssnnnnnnnnn         | date +%Y%m%d%H%M%S%N       | $(date +%Y%m%d%H%M%S%N)
Seconds since UNIX epoch:     | date +%s                   | $(date +%s)
Nanoseconds only:             | date +%N                   | $(date +%N)
Nanoseconds since UNIX epoch: | date +%s%N                 | $(date +%s%N)
ISO8601 UTC timestamp         | date --utc +%FT%TZ         | $(date --utc +%FT%TZ)
ISO8601 Local TZ timestamp    | date +%FT%T%Z              | $(date +%FT%T%Z)
EOD

If executed, it will produce the (obvious) output:

        Format/result         |       Command              |          Output
------------------------------+----------------------------+------------------------------
YY-MM-DD_hh:mm:ss             | date +%F_%T                | 2013-05-17_10:16:09
YYMMDD_hhmmss                 | date +%Y%m%d_%H%M%S        | 20130517_101609
YYMMDD_hhmmss (UTC version)   | date --utc +%Y%m%d_%H%M%SZ | 20130517_011609Z
YYMMDD_hhmmss (with local TZ) | date +%Y%m%d_%H%M%S%Z      | 20130517_101609JST
YYMMSShhmmss                  | date +%Y%m%d%H%M%S         | 20130517101609
YYMMSShhmmssnnnnnnnnn         | date +%Y%m%d%H%M%S%N       | 20130517101609418928482
Seconds since UNIX epoch:     | date +%s                   | 1368753369
Nanoseconds only:             | date +%N                   | 427187053
Nanoseconds since UNIX epoch: | date +%s%N                 | 1368753369431083605
ISO8601 UTC timestamp         | date --utc +%FT%TZ         | 2013-05-17T01:16:09Z
ISO8601 Local TZ timestamp    | date +%FT%T%Z              | 2013-05-17T10:16:09JST

7 thoughts on “Most common Bash date commands for timestamping

  1. Rich

    You nearly made me choke with those non-UTC timestamps! You want to add date --utc +%FT%TZ and date +%FT%T%Z? The “T” makes it a little harder to read at first, but it’s correct per ISO 8601.

    Reply
  2. zxq9 Post author

    @Rich
    Not a bad point. I didn’t include anything with timezones. I didn’t articulate this clearly, but the primary motivation for the post/script was being pestered with questions about how to produce filename-friendly timestamps in scripts — hence the lack of characters that are tricky on some operating systems (colons, in particular).

    Anyway, its easy to forget about timezones living in a country where there is only one! I’ve updated the post with your examples, because they are indeed useful.

    Thanks for the input!

    Reply
  3. Mac Fag

    Wow, for about 2-3 minutes I thought you were smart until I reached the following in your text:
    “# Now all you Mac fags can stop pestering me.”

    Now I just think you’re a complete loser.

    Reply
  4. zxq9 Post author

    @Mac Fag
    At the time I wrote this I was surrounded by the Mac Fag Clone Army (figuratively, at least). That comment was for them, but if you feel you belong in the same category you’re welcome to apply the label to yourself.

    Reply
  5. Marzipan

    “Mac Fag” might be a touch juvenile, but it clearly hit close to home for someone… i can’t imagine any other reason you left it in the comments than to troll.

    +1 for the Best in Show reference! Excellent demonstration why its stupid to try flaming someone on their own blog.

    Btw, writing this on my iPhone, which is actually quite annoying.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>