View all date formats, Quick Reference Help Alias

alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do F=${l/% */}; date +%$F:"|'"'"'${F//%n/ }'"'"'|${l#* }";done|sed "s/\ *|\ */|/g" |column -s "|" -t'
If you have used bash for any scripting, you've used the date command alot. It's perfect for using as a way to create filename's dynamically within aliases,functions, and commands like below.. This is actually an update to my first alias, since a few commenters (below) had good observations on what was wrong with my first command. # creating a date-based ssh-key for ssh-keygen -f ~/.ssh/`date +git-$USER@$HOSTNAME-%m-%d-%g` -C '' # /home/gpl/.ssh/ # create a tar+gzip backup of the current directory tar -czf $(date +$HOME/.backups/%m-%d-%g-%R-`sed -u 's/\//#/g' <<< $PWD`.tgz) . # tar -czf /home/gpl/.backups/04-22-10-01:13-#home#gpl#.rr#src.tgz . I personally find myself having to reference date --help quite a bit as a result. So this nice alias saves me a lot of time. This is one bdash mofo. Works in sh and bash (posix), but will likely need to be changed for other shells due to the parameter substitution going on.. Just extend the sed command, I prefer sed to pretty much everything anyways.. but it's always preferable to put in the extra effort to go for as much builtin use as you can. Otherwise it's not a top one-liner, it's a lazyboy recliner. Here's the old version: alias dateh='date --help|sed "/^ *%%/,/^ *%Z/!d;s/ \+/ /g"|while read l;do date "+ %${l/% */}_${l/% */}_${l#* }";done|column -s_ -t' This trick from my [ bash_profile ]
Sample Output
%%:     '%'                         a literal %
%a:     'Wed'                       locale's abbreviated weekday name (e.g., Sun)
%A:     'Wednesday'                 locale's full weekday name (e.g., Sunday)
%b:     'Mar'                       locale's abbreviated month name (e.g., Jan)
%B:     'March'                     locale's full month name (e.g., January)
%c:     'Wed Mar 27 23:50:06 2013'  locale's date and time (e.g., Thu Mar  3 23:05:25 2005)
%C:     '20'                        century; like 2013, except omit last two digits (e.g., 20)
%d:     '27'                        day of month (e.g., 01)
%D:     '03/27/13'                  date; same as 03/27/13
%e:     '27'                        day of month, space padded; same as 27
%F:     '2013-03-27'                full date; same as 2013-03-27
%g:     '13'                        last two digits of year of ISO week number (see 2013)
%G:     '2013'                      year of ISO week number (see 13); normally useful only with 13
%h:     'Mar'                       same as Mar
%H:     '23'                        hour (00..23)
%I:     '11'                        hour (01..12)
%j:     '086'                       day of year (001..366)
%k:     '23'                        hour, space padded ( 0..23); same as 23
%l:     '11'                        hour, space padded ( 1..12); same as 11
%m:     '03'                        month (01..12)
%M:     '50'                        minute (00..59)
%n:     ' '                         a newline
%N:     '853525313'                 nanoseconds (000000000..999999999)
%p:     'PM'                        locale's equivalent of either AM or PM; blank if not known
%P:     'pm'                        like PM, but lower case
%r:     '11:50:06 PM'               locale's 12-hour clock time (e.g., 11:11:04 PM)
%R:     '23:50'                     24-hour hour and minute; same as 23:50
%s:     '1364442606'                seconds since 1970-01-01 00:00:00 UTC
%S:     '06'                        second (00..60)
%t:     '       '                             a tab
%T:     '23:50:06'                  time; same as 23:50:06
%u:     '3'                         day of week (1..7); 1 is Monday
%U:     '12'                        week number of year, with Sunday as first day of week (00..53)
%V:     '13'                        ISO week number, with Monday as first day of week (01..53)
%w:     '3'                         day of week (0..6); 0 is Sunday
%W:     '12'                        week number of year, with Monday as first day of week (00..53)
%x:     '03/27/13'                  locale's date representation (e.g., 12/31/99)
%X:     '23:50:06'                  locale's time representation (e.g., 23:13:48)
%y:     '13'                        last two digits of year (00..99)
%Y:     '2013'                      year
%z:     '-0400'                     +hhmm numeric time zone (e.g., -0400)
%:z:    '-04:00'                    +hh:mm numeric time zone (e.g., -04:00)
%::z:   '-04:00:00'                 +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z:  '-04'                       numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z:     'EDT'                       alphabetic time zone abbreviation (e.g., EDT)

These Might Interest You

  • Prints out the list of date(1) format codes, for quick easy reference. dateformatcodes Show Sample Output

    alias dateformatcodes="date --help | sed -n '/^FORMAT/,/%Z/p'"
    defiantredpill · 2011-10-20 17:43:36 0
  • Not a discovery but a useful one nontheless. In the above example date format is 'yyyymmdd'. For other possible formats see 'man date'. This command can be also very convenient when aliased to some meaningful name: alias mkdd='mkdir $(date +%Y%m%d)'

    mkdir $(date +%Y%m%d)
    thebodzio · 2009-04-25 14:16:45 4
  • Once you get into advanced/optimized scripts, functions, or cli usage, you will use the sort command alot. The options are difficult to master/memorize however, and when you use sort commands as much as I do (some examples below), it's useful to have the help available with a simple alias. I love this alias as I never seem to remember all the options for sort, and I use sort like crazy (much better than uniq for example). # Sorts by file permissions find . -maxdepth 1 -printf '%.5m %10M %p\n' | sort -k1 -r -g -bS 20% 00761 drwxrw---x ./tmp 00755 drwxr-xr-x . 00701 drwx-----x ./askapache-m 00644 -rw-r--r-- ./.htaccess # Shows uniq history fast history 1000 | sed 's/^[0-9 ]*//' | sort -fubdS 50% exec bash -lxv export TERM=putty-256color Taken from my Show Sample Output

    alias sorth='sort --help|sed -n "/^ *-[^-]/s/^ *\(-[^ ]* -[^ ]*\) *\(.*\)/\1:\2/p"|column -ts":"'
    AskApache · 2010-06-10 21:30:31 0
  • On CentOS at least, date returns a boolean for the common date string formats, including YYYY-MM-DD. In the sample output, you can see various invalid dates returning 0 whereas a simple regex check would return 1 for the invalid dates. -d, --date=STRING display time described by STRING, not `now' The version of date on OS X does not appear to have this same option. Show Sample Output

    if date -d 2006-10-10 >> /dev/null 2>&1; then echo 1; else echo 0; fi
    SteveGoossens · 2013-01-10 10:35:15 0

What Others Think

I just use man date, as it explains the difference between e and f and u and w and U, V and W and others that come up identical with this command at certain moments. man date |grep %
houghi · 426 weeks and 2 days ago
I like this as it works with current time
oernii2 · 426 weeks and 2 days ago
Why did you duplicate the sample output? Can you edit it out of your main description?
SuperJediWombat · 426 weeks and 1 day ago
@SuperJedi got it. @houghi I upgraded the script to make it better than man, thanks for the push. Now save some keystrokes.
AskApache · 426 weeks ago
I prefer this version with less whitespace and line wrapping: alias dateh='date --help|sed -n "/^ *%%/,/^ *%Z/p"|while read l;do date "+ %${l/% */}:%t${l/% */}%n%t%t${l#* }";done' It also eliminates some odd quote placement and escaping in the original.
dennisw · 426 weeks ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: