Displays or sets the date.
Setting the date is often difficult for new users: the syntax can be strange and the man page can be confusing. Worse, it is different on the various Unix variants. To set August 25th 2003, 9:15 PM, requires:
I often install a script to help:
#!/bin/sh # Setdate for Linux echo -n "Enter Date or leave blank for `date +%m/%d/%y`: " read mydat echo -n "Enter Time or leave blank for `date +%H:%M`: " read mytime if [ "$mydat" != "" ] then mmdd=`echo $mydat | sed 's/\///g;s/..$//'` yr=`echo $mydat | sed 's/\///g;s/^....//'` else mmdd=`date +%m%d` yr=`date +%y` fi if [ "$mytime" != "" ] then newtime=`echo $mytime | sed 's/://g'` else newtime=`date +%H%M` fi date "$mmdd$newtime$yr"
As the above script shows, "date" can be very precise in its output.
On systems where you have it, GNU date ("date -d yesterday") or "datemath" is the easiest way to do such things as finding yesterday's or tomorrow's date. Without that, using Perl or other higher level languages is easier than forcing the issue with "date", but it can be done: https://aplawrence.com/Unix/yesterday.html.
With Perl and other languages, dates are held as they actually are on the system itself: as seconds since January, 1970. This makes date math easy: to get to tommorow, add one day's worth of seconds (60 * 60 * 24), and so on. For example:
#!/usr/bin/perl $t=time; print "Today is " . localtime($t); print "\nYesterday was " . localtime($t - (3600 * 24)); print "\nTomorrow will bes " . localtime($t + (3600 * 24));
Got something to add? Send me email.
More Articles by Tony Lawrence © 2011-07-06 Tony Lawrence
He who hasn't hacked assembly language as a youth has no heart. He who does as an adult has no brain. (John Moore)
Printer Friendly Version
date Copyright © December 2003 Tony Lawrence
Have you tried Searching this site?
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.
Contact us
Printer Friendly Version