date is a basic Unix command for getting or setting the current time and date on your system. Because it's the easiest way to get current time, this command is extensively used in Unix scripting.
Getting current time and date in your Unix system
The default usage of this command is simple and requires no additional command line parameters:
Wed Jun 11 11:43:52 IST 2008
Using templates to specify the desired date/time format
date command supports template system for printing the current time and date – so you can use it to specify the exact format for representing the time and date information – for example, only print out the day of a month or a current year instead of the whole default timestamp shown above.
Format is text string which begins with the + character, which consists of a number of special parameters starting with the % sign: %B, %d, etc. If you're using spaces or any other elements in your format string, you need to use double quotes as well.
Here's an example of specifying a format for the date command:
ubuntu$ date "+%B %d, %Y"
June 11, 2008
In this example, the %B represents the full name of the current month, %d is the day of the month, and %Y is the four-digit representation of the current year.
Here are some of the most useful parameters used for date format specification:
%a locale’s abbreviated weekday name (e.g., Sun) %A locale’s full weekday name (e.g., Sunday) %b locale’s abbreviated month name (e.g., Jan) %B locale’s full month name (e.g., January) %c locale’s date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 21) %d day of month (e.g, 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour ( 0..23) %l hour ( 1..12) %m month (01..12) %M minute (00..59) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %w day of week (0..6); 0 is Sunday %x locale’s date representation (e.g., 12/31/99) %X locale’s time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year
Unix date manipulations
If you're interested in confirming any date/time information about dates in the past or in the future, you will find all the answers in my article here: Easy Date Calculations in Unix Scripts.
Unix epoch time
Since time in most Unix systems is calculated against the Unix epoch (January 1, 1970), you can use date command to confirm the exact number of seconds elapsed since this time on your Unix system:
ubuntu$ date +%s