cmd:date 

Usage 
$ date -I
2005-08-04

$ date -Iseconds
2005-08-04T17:24:39-0400

$ date -Iseconds -u
2007-06-21T13:10:18+0000

$ date '+%H:%M:%S'
17:24:41

$ date '+%Y%m%d'
20020321

$ date '+%y%m%d'
020321
Advanced Usages 
# tomorrow
date --date=tomorrow

# yesterday
date --date='-1 day'

# last month
date --date='-1 month'

# next month
date --date='+1 month' '+%m %Y'
Set 
$ printf 'Set current time: date '; date '+%m%d%H%M'
Set current time: date 03281506

$ printf 'Set current time: date '; date '+%m%d%H%M%y'
Set current time: date 0101184403

% date 03181019
Mon Mar 18 10:19:00 CST 2002

$  date
Mon Mar 18 10:19:03 CST 2002
DESCRIPTION 

Display the current time in the given FORMAT, or set the system date.

%Y     year (1970...)
%y     last two digits of year (00..99)
%m     month (01..12)
%d     day of month (01..31)
%H     hour (00..23)
%I     hour (01..12)
%M     minute (00..59)
%S     second (00..60); the 60 is for a leap  second

day of week 

Every system should have gnu date:

$ date -d "Sun Jul 25 09:10:27 PDT 1999" '+%w %a %A'
0 Sun Sunday

Dan Mercer

tomorrow 

use GNU date (part of GNU sh-utils) with:

date --date=tomorrow +%d

or use mktime (http://www.weirdways.com/technogeek/source/mktime/index.html)

mktime -d +1 -F %d

FYI, You can also use mktime to directly discover what the last day of the current month is:

mktime -m +1 -d 0 -F %d

Ken Pizzini

Calculate next business day 

Newsgroups:  comp.unix.shell
Date:        Thu, 13 Oct 2005 16:17:51 +0200
> Hi, I need an algorightm (using bash script) to calculate next business
> day (in mm/dd/yyyy format). I'm not worried about Thanksgiving,
> Christmas etc. I just need to find out next business day so that it
> falls between Monday and Friday, given today.

with GNU date and bash:

d=1
while (($(date -d "$d days" +%u) >= 6)); do ((++d)); done
date -d "$d days" +%d/%m/%Y

Steffen Schuler

Calculate next business day 

> I just need to find out next business day so that it
> falls between Monday and Friday, given today.
[ $(LC_ALL=C date +%u) -gt 5 ] && echo "no work tomorrow" || echo "work tomorrow :-("

May be a way. Hoping more portable then GNU date and GNU bash. Setting LC_ALL should not be neccesary.

Calculate next business day 

> Hi, I need an algorightm (using bash script) to calculate next business
> day (in mm/dd/yyyy format).

All scripts I use take the date in YYYY-MM-DD format. It's easy enough to convert to your illogical format when you're finished.

Basically:

while :
do
   case $dow in
       [12345]) break ;;
       *) get_tomorrow        ## Use a function or GNU date
          get_dow $date   ## Functions available; see below
          ;;
   esac
done
echo $date   ## Format according to taste
> I'm not worried about Thanksgiving, Christmas etc. I just need to
> find out next business day so that it falls between Monday and
> Friday, given today.

For information on date arithmetic, see the comp.unix.shell FAQ <http://home.comcast.net/~j.p.h/cus-faq.html#6>, or see Chapter 8 of my book (see my .sig for the URL; that chapter is available on-line).

Chris F.A. Johnson                     <http://cfaj.freeshell.org>
==================================================================
Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress
<http://www.torfree.net/~chris/books/cfaj/ssr.html>

cript with date operation?? 

Newsgroups:  gmane.linux.debian.user
Date:        Sat, 13 May 2006 14:04:01 -0400
> Is there any nice tool to operate dates at the console?
> thanks!

It's perhaps a bit too heavy, but I always use Perl's DateManip for that: http://search.cpan.org/~sbeck/DateManip-5.44/Manip.pod

I had to write a script a while back that computed "one week before today"; DateManip's way of writing that is quite literate and quite easy to code:

LASTINTERVAL=`perl -e "use Date::Manip; print UnixDate(DateCalc(\"now\",\"-1 week\"), \"%d/%b/%Y\")"`

In general, I should note that "perl -e" is super-awesome.

The alternative, using just the command-line 'date' tool, is too cumbersome. Perl's actually the easiest here, I've found.

Stephen R. Laniel

cript with date operation?? 

> I am working on a script that needs to compute days. It needs to know
> how many days have gone since some given date. for example, if I run it
> today, it will need to know how many days have passed since, say, Feb,
> 02, 2006. And add the corresponding number to a variable.

Since you're doing this in a script, it presumably doesn't need to be done in a single line. I'd start with

date -d "Feb 2, 2006" +"%s"

to get the number of seconds since the epoch for midnight on the date specified. Then I'd do

date +"%s"

to get the same for the current time. Subtract those, and you get the number of seconds since Feb 2, 2006. There are 86400 seconds in a day. Divide, throw out the remainder, and you've got the number of days since Feb 2, 2006.

If you *must* have a one-liner:

expr \( `date +"%s"` - `date -d "Feb 2, 2006" +"%s"` \) / 86400

Michael A. Marsh

cript with date operation?? 

On 5/13/06, Stephen R Laniel wrote:

> That sort of work is precisely why I do it in Perl. Because
> then you start getting into messiness with leap years,
> timezones, etc., etc., etc. There's a reason that time
> libraries are hard to write. :-) Perl's done all the work
> for you; be lazy.

What do leap years have to do with it? Leap *seconds*, maybe, but I'm willing to be off on the count of days by a few seconds. If timezones are a real problem, you can always specify the timezone after the year. date has done all the work for you.

Michael A. Marsh