Unix tools introduced. Today: date

The unix tool date can be used to print the current date. It also can be used to calculate time spans or to reformat time strings.

Examples:
1. Print the current date in a conventional form

date +"%d. %B %Y"

2. Use certain locale. Find supported list in /usr/share/i18n/SUPPORTED

LANG=de_DE.UTF-8 date +"%d. %B %Y"

3. Print an arbitrary (but valid) timestring in a conventional form

date -d 2003-11-22 +"%d. %B %Y"

4. Print the current date as seconds since 1970 (unix epoche)

date +%s

5. Calculate the difference of two dates in days. Explanation can be found here.

A="2002-10-20"
B="2003-11-22"
echo $(( ($(date -d $B +%s) - $(date -d $A +%s)) / 86400 )) days

6. Print seconds after 1970 (unix epoche) in a conventional form.

TZ=UTC LANG=en_EN.UTF-8  date -d@0 +"%H:%M:%S %d. %B %Y"

Note: if TZ=UTC is left out, date will add or subtract an offset in accordance to systems timezone.

7. Find timezone offset

OFF_1=$(date -d '1 Jan' +%z)
OFF_7=$(date -d '1 Jul' +%z)
echo $OFF_1 $OFF_7

 

Leave a Reply