Hack 9. Display Current Date and Time in a Specific Format

by Ramesh

Following are different ways of displaying the current date and time in various formats.

  1. $ date
  2. Thu Jan 1 08:19:23 PST 2009
  3.  
  4. $ date --date="now"
  5. Thu Jan 1 08:20:05 PST 2009
  6.  
  7. $ date --date="today"
  8. Thu Jan 1 08:20:12 PST 2009
  9.  
  10. $ date --date='1970-01-01 00:00:01 UTC +5 hours' +%s
  11. 18001
  12.  
  13. $ date '+Current Date: %m/%d/%y%nCurrent Time:%H:%M:%S'
  14. Current Date: 01/01/09
  15. Current Time:08:21:41
  16.  
  17. $ date +"%d-%m-%Y"
  18. 01-01-2009
  19.  
  20. $ date +"%d/%m/%Y"
  21. 01/01/2009
  22.  
  23. $ date +"%A,%B %d %Y"
  24. Thursday,January 01 2009

Following are the different format options you can pass to the date command:

  • %D date (mm/dd/yy)
  • %d day of month (01..31)
  • %m month (01..12)
  • %y last two digits of year (00..99)
  • %a locale’s abbreviated weekday name (Sun..Sat)
  • %A locale’s full weekday name, variable length (Sunday..Saturday)
  • %b locale’s abbreviated month name (Jan..Dec)
  • %B locale’s full month name, variable length (January..December)
  • %H hour (00..23)
  • %I hour (01..12)
  • %Y year (1970…)