# Date function question

**URL:** <https://community.unix.com/t/date-function-question/139601>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [September 17, 2001, 2:20am UTC](https://community.unix.com/t/date-function-question/139601 "2001-09-17T02:20:12Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![crpalermo](https://community.unix.com/letter_avatar/crpalermo/32/6_449c533f586939b6e5f246632a46421f.png) [@crpalermo](https://community.unix.com/u/crpalermo)\
**Post date:** [September 17, 2001, 2:20am UTC](https://community.unix.com/t/date-function-question/139601/1 "2001-09-17T02:20:12Z")

</div>

hi guys!  
just want to ask if you could help me with the sript i'm working on. i need to automatically generate a summarized report everyday for all transactions the day before and ftp it to another machine. my only problem is that i need to name the file as the date yesterday. for example if i would generate a report today (09172001), the filename should be 09162001.txt, i've read the man for date but i haven't got a clue as to what to do.  
please help.  
thanks!

---

<div class="post-metadata">

**Author:** ![vrjoshi](https://community.unix.com/letter_avatar/vrjoshi/32/6_449c533f586939b6e5f246632a46421f.png) [@vrjoshi](https://community.unix.com/u/vrjoshi)\
**Post date:** [September 17, 2001, 6:10am UTC](https://community.unix.com/t/date-function-question/139601/2 "2001-09-17T06:10:04Z")

</div>

Dear friend

write a few lines in the shell as follows:

bye using date format command store today's date value in variable dt

hence if todays date is 09282001 in mmddyyyy format  
dt='09282001'  
now store value of dt to a file  
echo $dt\>file1  
now cut value of today's date in variable v1  
v1=`cut -c3-4 file1`  
now find out value of previous date by using expr command  
v2=`expr $v1 -1`  
now prepare the full date by replacing 3rd and 4th chr of file1 by value of v1

Of course you will also have to add validations for last date of months and for february also  
hope expr command will help you

Wish you all the best

yours friendly  
VRJoshi

---

<div class="post-metadata">

**Author:** ![Optimus\_P](https://community.unix.com/letter_avatar/optimus_p/32/6_449c533f586939b6e5f246632a46421f.png) [@Optimus\_P](https://community.unix.com/u/Optimus_P)\
**Post date:** [September 17, 2001, 9:42am UTC](https://community.unix.com/t/date-function-question/139601/3 "2001-09-17T09:42:34Z")

</div>

just a thought. I dont see much use of processing data and labeling it the day befor. why not just run it from cron @ end of bussiness day or even at 11:55pm then u dont have to worry about adding and subtracting from the date.?

also if you look at the date man page at the bottom there are plenty of examples of getting the date in a format you would like.

---

<div class="post-metadata">

**Author:** ![PxT](https://community.unix.com/user_avatar/community.unix.com/pxt/32/10_2.png) [@PxT](https://community.unix.com/u/PxT)\
**Post date:** [September 17, 2001, 1:41pm UTC](https://community.unix.com/t/date-function-question/139601/4 "2001-09-17T13:41:15Z")

</div>

> [@](#):
>
> Originally posted by Optimus\_P  
> why not just run it from cron @ end of bussiness day or even at 11:55pm then u dont have to worry about adding and subtracting from the date.?

Then what if something happens at 11:56, that wont be reflected in the log file. One solution might be to do something like:

```plaintext
DATESTRING=`date +%m%d%Y`

sleep 120

# run report, redirect to $DATESTRING.txt

```

Run it at 11:59pm from crontab...

---

<div class="post-metadata">

**Author:** ![Optimus\_P](https://community.unix.com/letter_avatar/optimus_p/32/6_449c533f586939b6e5f246632a46421f.png) [@Optimus\_P](https://community.unix.com/u/Optimus_P)\
**Post date:** [September 17, 2001, 1:49pm UTC](https://community.unix.com/t/date-function-question/139601/5 "2001-09-17T13:49:16Z")

</div>

ptx: i agree but that is why i said run it late.ie 11:56. most business should be done.

but w/ your example i completly agree and it is a great idea to get the date string first then sleep for 2 mins then finish.

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [September 17, 2001, 3:10pm UTC](https://community.unix.com/t/date-function-question/139601/6 "2001-09-17T15:10:36Z")

</div>

Well, I'm bored this afternoon...so

```nohighlight
#!/usr/bin/ksh

set -A months 0 31 28 31 30 31 30 31 31 30 31 30 31
typeset -Z2 month_y day_y

today=$(date +%m%d%Y)

month_t=${today%??????}
year_t=${today#????}
day_t=${today%????}
day_t=${day_t#??}

((year_t % 100)) && ((leap = !(year_t % 4))) || ((leap = !(year_t % 400)))
((leap)) && ((months[2] = months[2] + 1))

((year_y = year_t))
((month_y = month_t))
((day_y = day_t - 1))

if ((!day_y)) ; then
   ((month_y = month_y - 1))
   if ((!month_y)) ; then
      ((year_y = year_y-1))
      ((month_y = 12))
   fi
   ((day_y = months[month_y]))
fi

yesterday=${month_y}${day_y}${year_y}
echo yesterday = $yesterday
	
exit 0

```

I think I got this right...I only tested it lightly.

---

<div class="post-metadata">

**Author:** ![crpalermo](https://community.unix.com/letter_avatar/crpalermo/32/6_449c533f586939b6e5f246632a46421f.png) [@crpalermo](https://community.unix.com/u/crpalermo)\
**Post date:** [September 18, 2001, 12:04am UTC](https://community.unix.com/t/date-function-question/139601/7 "2001-09-18T00:04:42Z")

</div>

Thanks for all the help guys! I highly appreciate it

---

<div class="post-metadata">

**Author:** ![kandanmv](https://community.unix.com/letter_avatar/kandanmv/32/6_449c533f586939b6e5f246632a46421f.png) [@kandanmv](https://community.unix.com/u/kandanmv)\
**Post date:** [September 25, 2001, 11:52pm UTC](https://community.unix.com/t/date-function-question/139601/8 "2001-09-25T23:52:10Z")

</div>

Hi,

sorry for the delayed response, just figured out.

u can use:

echo `echo '*time-0t86400=Y' | /usr/bin/adb -k | tail -2`

to get yesterdays date.

Thanks,

---

<div class="post-metadata">

**Author:** ![Perderabo](https://community.unix.com/user_avatar/community.unix.com/perderabo/32/27_2.png) [@Perderabo](https://community.unix.com/u/Perderabo)\
**Post date:** [September 26, 2001, 12:00pm UTC](https://community.unix.com/t/date-function-question/139601/9 "2001-09-26T12:00:21Z")

</div>

Whoa! Interesting approach, kandanmv! And I didn't know adb defaults the objectfile and corefile with -k like that. But it does seem to work.

---

<div class="post-metadata">

**Author:** ![kandanmv](https://community.unix.com/letter_avatar/kandanmv/32/6_449c533f586939b6e5f246632a46421f.png) [@kandanmv](https://community.unix.com/u/kandanmv)\
**Post date:** [September 26, 2001, 11:15pm UTC](https://community.unix.com/t/date-function-question/139601/10 "2001-09-26T23:15:31Z")

</div>

gee thanks.  
but the easiest way would be to use perl

# perl -e 'printf "%s", scalar localtime(time-86400)'

regds,

---

<div class="post-metadata">

**Author:** ![PxT](https://community.unix.com/user_avatar/community.unix.com/pxt/32/10_2.png) [@PxT](https://community.unix.com/u/PxT)\
**Post date:** [September 27, 2001, 4:37pm UTC](https://community.unix.com/t/date-function-question/139601/11 "2001-09-27T16:37:35Z")

</div>

Or save yourself a few bytes:

perl -e 'print scalar localtime(time-86400)'

---

<div class="post-metadata">

**Author:** ![akrahul](https://community.unix.com/letter_avatar/akrahul/32/6_449c533f586939b6e5f246632a46421f.png) [@akrahul](https://community.unix.com/u/akrahul)\
**Post date:** [July 22, 2003, 6:33am UTC](https://community.unix.com/t/date-function-question/139601/12 "2003-07-22T06:33:29Z")

</div>

Thanks, kandanmv and PxT. The Perl commands are useful.

---

<div class="post-metadata">

**Author:** ![nitin](https://community.unix.com/letter_avatar/nitin/32/6_449c533f586939b6e5f246632a46421f.png) [@nitin](https://community.unix.com/u/nitin)\
**Post date:** [October 13, 2003, 3:38am UTC](https://community.unix.com/t/date-function-question/139601/13 "2003-10-13T03:38:39Z")

</div>

Perderabo's code was very helpful. I used it adn worked fine, except that my linux box was giving me errors on set and typset commands:confused:, so I just took them out!  
Thanks  
Nitin 😃
