Help on Dates in Shell Scripting

Hi Experts,

I am using the below code to get the previous day based on the date given as a input.

#!/usr/bin/ksh

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
echo $yest

Output: 20130714

How can i display the same output in 14/07/2013, i tired '+%d/%m/%y'` but i am getting error.

also how can i get the first day of the month based on the date which is fetched $yest.

Any help.

Thanks

Do you have GNU date? just check man page of date if u have GNU date then you have option to get yesterday's date..

If not you have to modify TZ to get the desired date.. date - 1 wont work :slight_smile:

If your date command supports the -d option (gnu date)
you can do:

Yesterday:

date -d "-1 day" +%d/%m/%Y

1st of month:

YM=`date +%Y%m`
date -d ${YM}01 +%d/%m/%Y

Hi Experts,

when i run the command in unix prompt: i got the below error:

date -d "-1 day" +%d/%m/%Y
date: illegal option -- d
Usage: date [-u] [+format]
       date [-u] [mmddhhmm[[cc]yy]]
       date [-a [-]sss.fff]

can you help me to sort this.

regards,

OK you don't have gnu date you might have to use perl

yesterday.pl

#!/bin/perl
use POSIX; print strftime('%d/%m/%Y', localtime(time() - 24*60*60));

first.pl

#!/bin/perl
use POSIX;
my $year, $month;
($year, $month) = split(/\s/, strftime('%Y %m', localtime(time())));
printf strftime('%d/%m/%Y', localtime(mktime(0,0,0,1,$month-1,$year-1900,0,0)));

Hi Experts,

I want to use in unix script.

any help/suggestions

regards,

For your first requirement, this should work

$ date -j -f "%s" $(($(date "+%s") - 86400))  "+%d/%m/%Y"
15/07/2013

Hi Experts,

GNU date is not supporting in my UNIX prompt, so based on today's date how can i get the first day' of the month. i want the first date of the month.

for example:

if current date is 16/07/2013, i want the 01/07/2013. i mean first of the month.

Thanks,

I am not sure if this is what you need..just check out..

$ date "+%d/%m/%Y"  | awk -F"/" '{print "01"FS$2FS$3}' 
01/07/2013

Hi Experts,

Thanks, this is working for current month, if we pass a different date like 12/06/2013, will this work?

how can we modify the code to take any date and get the first day of that month.

for example:

if we have input as 12/06/2013,the first date of the month is 01/06/2013.

how can we do this?

thanks in davance.

regards,

$ echo "12/06/2013" | awk -F"/" '{print "01"FS$2FS$3}' 
01/06/2013
$ echo "31/12/2013" | awk -F"/" '{print "01"FS$2FS$3}' 
01/12/2013

Hi Experts,

can i store this value in variable like.

inputdate=12/06/2013
date= echo "inputdate" | awk -F"/" '{print "01"FS$2FS$3}'

echo $date

output: Expected
01/06/2013

regards,

Yes. I would suggest you to give an attempt and try something yourself before posting.

date=$(echo $inputdate | awk -F"/" '{print "01"FS$2FS$3}')

Hi Experts,

I tired this.

#!/bin/ksh

inputdate=15/07/2013

year=`expr substr $inputdate 7 4`
month=`expr substr $inputdate 4 2`

FROM_DATE=01/$month/$year
echo $month
echo $year
echo $FROM_DATE

let me know if correct?

---------- Post updated at 02:01 AM ---------- Previous update was at 01:26 AM ----------

Hi Experts,

i am using the below code get the date of previous day.

#!/usr/bin/ksh

datestamp=`date '+%Y%m%d'`
yest=$((datestamp -1))
echo $yest

When i execute the code i am getting output as:

20130715

What i am trying here is, based on the date passed i am fetching previus day's date.

for example:

If date passed is date = 16/07/2013, i need date of previous day

15/07/2013 but now i am getting output as 20130715, how can i convert this to format 15/07/2013

Thanks in advance.

seems, here is the issue

datestamp=`date '+%Y%m%d'

( mentioned as Year,Month and date)

please change and try.

$ dt=20130715
$ mmdd=${dt#????}
$ echo ${mmdd#??}"/"${mmdd%??}"/"${dt%????}
15/07/2013

Hi Experts,

#!/usr/bin/ksh

date = 16/07/2013

datestamp=$date '+%d/%m/%Y'
yest=$((datestamp -1))
echo $previous_day

i am getting error, how can i get the pervious days date based on the input date, any suggestions please.

My input date is

#!/usr/bin/ksh

date = 18/06/2013

i want previous date 17/06/2013.

thanks