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 askapache.github.com ssh-keygen -f ~/.ssh/`date +git-$USER@$HOSTNAME-%m-%d-%g` -C 'webmaster@askapache.com' # /home/gpl/.ssh/git-gplnet@askapache.github.com-04-22-10 # 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 [ http://www.askapache.com/linux-unix/bash_profile-functions-advanced-shell.html 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)

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 · 443 weeks and 4 days ago
I like this as it works with current time
oernii2 · 443 weeks and 4 days ago
Why did you duplicate the sample output? Can you edit it out of your main description?
SuperJediWombat · 443 weeks and 3 days ago
@SuperJedi got it. @houghi I upgraded the script to make it better than man, thanks for the push. Now save some keystrokes.
AskApache · 443 weeks and 2 days 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 · 443 weeks and 2 days 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: