How to find the first day of previous month in unix mmddyyyy format?
example : today is 07052007 (in mmddyyyy)
output sud be 06012007
thanks
mohapatra
How to find the first day of previous month in unix mmddyyyy format?
example : today is 07052007 (in mmddyyyy)
output sud be 06012007
thanks
mohapatra
This should get you closer. Try to check for day=1
THIS_MONTH=`date +%m`
THIS_YEAR=`date +%Y`
LAST_MONTH=`expr $THIS_MONTH - 1`
if [ $LAST_MONTH = 0 ]
then
LAST_MONTH=12
THIS_YEAR=`expr $THIS_YEAR - 1`
fi
cal $LAST_MONTH $THIS_YEAR | head -3 | tail -2
$ set -A lastmo $(datecalc -a $(date "+%Y %m") 1 - 1)
$ lastmo[2]=1
$ printf "%04d%02d%02d\n" ${lastmo[*]}
20070501
$
In te above example , what is datecalc ?
The datecalc script perderabo put in the FAQ section - the one section we keep asking you to read.
Thanks a lot for providing the solution . .But i want some problems as described below.
set -A lastmo $(datecalc -a $(date "+%Y %m") 1 - 1)
lastmo[2]=1
printf "%04d%02d%02d\n" ${lastmo[*]}
-> How can i modify so that the output will be in mmddyyyy format .
THIS_MONTH=`date +%m`
THIS_YEAR=`date +%Y`
LAST_MONTH=`expr $THIS_MONTH - 1`
if [ $LAST_MONTH = 0 ]
then
LAST_MONTH=12
THIS_YEAR=`expr $THIS_YEAR - 1`
fi
cal $LAST_MONTH $THIS_YEAR | head -3 | tail -2
-> the output should be in mmddyyyy format as well...
Can somebody please help .
Mohapatra,
Just concatenate the fields together:
typeset -R8 mMMDDYYYY
...
mMMDDYYYY='0'$LAST_MONTH'01'$THIS_YEAR
In January, LAST_MONTH and THIS_YEAR will not work right.
Perderabo,
I tested Nimo's solutions and couldn't find any problems.
Could you please advise?
Thanks.
Opps, my mistake. Despite the variable name, THIS_YEAR will be set to last year in that case.