Monday, April 2, 2012

date & GNU date



  date command in UNIX is used more often to create file names, rather than to check the system date. For any of our process, if we are in need of a unique file name, then one of the best options is to have the date and time as part of the file name.

 Let us see the usage of date command:

1. date command by default gives the system date and time.
$ date
Tue Mar 27 15:42:24 IST 2012
2. Date command has many switches with which we can extract the components of date separately.
$ date '+%Y%m%d'
20120327
     For example, the above command gives the 'year'(%Y), 'month'(%m) and 'date'(%d). Say, if we want the year component alone from the date command:

     $ date '+%Y'

3. Some format specifiers can also be added along with the date command as shown with the "-" below.
$ date '+%Y-%m-%d'
2012-03-27
4. This one gives the year, month, date, hours, minutes and seconds.
$ date '+%Y%m%d%H%M%S'
20120327154527
5.  A typical example in shell scripts how we use the date command to prepare a file name.
$ DT=`date '+%Y%m%d%H%M%S'`
$ FILE=VD_DAT_${DT}
$ echo $FILE
VD_DAT_20120327154618
       First, the date command output is taken in a variable DT.
$ FILE=VD_DAT_${DT}.txt
$ echo $FILE
VD_DAT_20120327154618.txt
GNU Date:
      If the date in your system is GNU date, the options you get in GNU date makes your life a breeze. The many difficult date computations are easily made with the "-d" option. Systems with GNU date will always be at advantage.

1. To get the date - 10mins. The "-d" switch allows us to back date by minutes, hours, days, months and years too.
$ date -d "10 mins ago"
Tue Mar 27 15:39:09 IST 2012
2. To get the date time  2  hours back:
$ date -d "2 hours ago"
Tue Mar 27 13:52:14 IST 2012
3. In the same lines, to get yesterdays date:
$ date -d "1 day ago"
Mon Feb 27 15:52:30 IST 2012
4. A combination of all the above meaning we can use multiple components such as days, months, hours, etc.
$ date -d "2 years 1 month 2 hours 30 mins ago"
Sun Apr 27 18:14:12 IST 2014
5. Similarly, we can have dates in future as well. All that needs to be done is to remove the word "ago". Say, to get a datetime 40 mins in advance:
$ date -d "40 mins"
Mon Apr  2 15:53:25 IST 2012
All this command does is adds 40 mins to the current date.

6. Similarly, to get current date + 1day OR to get tomorrow's date:
$ date -d "1 day"
Tue Apr  3 15:21:15 IST 2012
Script to do arithmetic on Dates:

     Even with the GNU date, the kind of date calculations you can do is pretty limited. Calculations like to find the difference between 2 dates are not possible using the date command. One of the most popular scripts in the Unix.com forum is the one which does arithmetic on dates, or in other words, date calculations. Some of the important things which the date calculation script does is:
  • Difference between 2 dates
  • Adding a number to a date
  • Converting a date to day of the week
  • Display the name of the day for a given date
  • Convert a date to a julian day number.
 The script for this date calculation is : datecalc. The beauty of this script is it is written purely using internal commands, without using a single external command.

1 comment:

  1. This is such a great resource that you providing and you give it away for free.
    Best Romantic Dinner

    ReplyDelete