Link to home
Start Free TrialLog in
Avatar of celtician
celticianFlag for American Samoa

asked on

Unix, date minus 1, 2 or 3 days (last working day)

I got the next expression that gives me the current date just like i need it (ddmmyyyy) :

$(date "+%d%m%Y")

Today is monday, i need the last working day (friday) so i need $(date "+%d%m%Y") minus 3 days

tomorro i will need $(date "+%d%m%Y") minus 1 day (referring to today)

how should i proceed to get those dates using that expression?

This is my machine OS version:

SunOS 5.9 Generic_Virtual sun4u sparc sun4v
Avatar of omarfarid
omarfarid
Flag of United Arab Emirates image

check the day name of today and if it is Mon then - 3 days else - 1 day
Use the date %s which number of sevo ss since epoch/UNIX timestamp. 1/1/1970 00:00:00 GMT.
You can the subtract the 3*24*3600 and convert it to the display you want.

Must you use she'll, or would a command line using perl work as well?
If you have a scripting language, such as PHP, this article shows how to make the calculations, taking into account weekends and holidays.  You can adapt the design shown in Practical Application #9.
Avatar of celtician

ASKER

Im using just unix a simple ksh script

check the day name of today and if it is Mon then - 3 days else - 1 day

How do i get -3 days and -1 day?

what function shall i use?

if using $(date "+%d%m%Y")  i get 24052016

what expression /function should i write to get 23052015 ??
You can use the worded "last friday"

as covered in http://blog.midnightmonk.com/85/bash/bash-date-manipulation.shtml

date --date="Last Friday" +"%d%m%Y"

it will return the date of the prior friday in the format you want..
to get day name run

date +%A

to subtract, try below command on your system:

date --date="3 days ago" "+%d%m%Y"

otherwise you need to install GNU date on your system.
Often gnu date might be installed in /usr/sfw/
Pkginfo -I | grep -i date
To see whether you have the sunfreeware package.
I used the next expression to get exactly the same i was asking for :

date "+%a %d%m%Y" | read dt

dt=$(perl -e 'use POSIX;print strftime "%d%m%Y",localtime time-259200;')

thanks anyway for the answers!!!
ASKER CERTIFIED SOLUTION
Avatar of arnold
arnold
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i used that expression and it worked so far arnold...  i used two different localtime amounts to be substracted, for 1 and 3 days respectively.
Not sure to which expression you refer, the fixed 259200 explicitly means three days ago. If you need the logic to no matter when this runs to reflect the date as last Friday , additional logic is needed to determine today and then adjust the time accordingly.