get yesterday in yyyymmdd format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get yesterday in yyyymmdd format
# 8  
Old 07-19-2005
Try...
Code:
Yesterday=$(perl -e '@y=localtime(time()-86400);printf "%04d%02d%02d",$y[5]+1900,$y[4]+1,$y[3];')

# 9  
Old 12-05-2007
Getting past few days

Quote:
Originally Posted by aaron_fong
how can i get yesterday in yyyymmdd format? Smilie
Modified the codes from: https://www.unix.com/unix-dummies-que...ming-date.html
to give at least past few days (tested till 10). Just to share.

Code:
date '+%m %d %Y' | 
{ 
read MONTH DAY YEAR
MINUS=10
TAIL_NO=`expr "$MINUS" - "4"` 
DAY=`expr "$DAY" - "$MINUS"`

#echo "DAY: "$DAY
#echo "TAIL: "$TAIL_NO

if [ "$DAY" -lt 1 ]
then 
       MONTH=`expr "$MONTH" - 1` 
                case "$MONTH" in 
                        0) 
                           MONTH=12 
                           YEAR=`expr "$YEAR" - 1` 
                        ;; 
                esac 
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -$TAIL_NO | head -1` 
fi 

if [ $MONTH -lt 10 ]
then
	MONTH="0"${MONTH}
fi
if [ $DAY -lt 10 ]
then
	DAY="0"${DAY}
fi
date
echo "${MINUS} day(s) ago was: ${YEAR}${MONTH}${DAY}" 
}

# 10  
Old 12-05-2007
I found this on the Internet once:

YESTERDAY=`TZ=GMT+24 date "+%Y%m%d"`

Works great for ksh in HP-UX 11i.
# 11  
Old 12-05-2007
For FreeBSD
Code:
date -v -1d "+%Y%m%d"

# 12  
Old 12-05-2007
This doesn't work on 02/01/2008 going back 1 day..

tail: unrecognized option `--3'
Try `tail --help' for more information.
tmp2.sh: line 27: [: -lt: unary operator expected
Fri Feb 1 15:03:29 CST 2008
1 day(s) ago was: 200801
MSIConv:~ # date
Fri Feb 1 15:03:40 CST 2008



Quote:
Originally Posted by zarn
Modified the codes from: https://www.unix.com/unix-dummies-que...ming-date.html
to give at least past few days (tested till 10). Just to share.

Code:
date '+%m %d %Y' | 
{ 
read MONTH DAY YEAR
MINUS=10
TAIL_NO=`expr "$MINUS" - "4"` 
DAY=`expr "$DAY" - "$MINUS"`

#echo "DAY: "$DAY
#echo "TAIL: "$TAIL_NO

if [ "$DAY" -lt 1 ]
then 
       MONTH=`expr "$MONTH" - 1` 
                case "$MONTH" in 
                        0) 
                           MONTH=12 
                           YEAR=`expr "$YEAR" - 1` 
                        ;; 
                esac 
        DAY=`cal $MONTH $YEAR | grep . | fmt -1 | tail -$TAIL_NO | head -1` 
fi 

if [ $MONTH -lt 10 ]
then
	MONTH="0"${MONTH}
fi
if [ $DAY -lt 10 ]
then
	DAY="0"${DAY}
fi
date
echo "${MINUS} day(s) ago was: ${YEAR}${MONTH}${DAY}" 
}

# 13  
Old 03-14-2008
Java Another way

date '+20%y/%m/%d' | awk -F"/" '{print $1$2($3-1)}'

output will be yesterday in YYYYMMDD format
# 14  
Old 03-14-2008
arun
try your command on January 1 :
Code:
echo "2008/01/01" | awk -F"/" '{print $1$2($3-1)}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to conver YYYYMMDD to MJD format?

I would like to convert YYYYMMDD to MJD format for eg 20041223 converted to 53362 in MJD format i got logic from javascript to covert it to MJD which is long enough function YMDtoMJD (year, month, day) { var year; var monthi var day; var yr = Math.floor (year); ... (1 Reply)
Discussion started by: mandalmanas24
1 Replies

2. Shell Programming and Scripting

Date format to be changed from DDMMYYYY to YYYYMMDD

My requirement is:- there will be files at a location each day with the date format DDMMYYYY. Novawise_Activity_Call_Notes_04022013.txt Novawise_Activity_Inbound_04022013.txt Novawise_Activity_Inbound_05022013.txt Novawise_Activity_Call_Notes_05022013.txt... (8 Replies)
Discussion started by: djrulz123
8 Replies

3. Shell Programming and Scripting

Validating date in yyyymmdd format using PERL

Hi all, i had a code where in user will enter a date in yyyymmdd format.. i didnt use any validation for the date and now the problem is if a user enters date instead of month after year it is proceeding with the code.. like if the date is 20120426 and if the user enters 20122604 it... (4 Replies)
Discussion started by: smarty86
4 Replies

4. Solaris

Date after 5 dates in YYYYMMDD format

Hi Experts, How to get date 5 days after current date in YYYYMMDD format? How do we compare date in YYYYMMDD format? Thanks (1 Reply)
Discussion started by: needyourhelp10
1 Replies

5. Shell Programming and Scripting

Change the date format from mmddyyyy to yyyymmdd

How do I change the date format from mmddyyyy to yyyymmdd in PERL. Can any one help me please. (3 Replies)
Discussion started by: thankful123
3 Replies

6. Shell Programming and Scripting

Find the next day from 20100303 (YYYYMMDD format)

Hi , i have doubt in bash shell script.. for example my file name is sampledate.sh given command = ./sampledate.sh -d 20100303 -f karthi how to find the next day from 20100303 (YYYYMMDD format) After getting the next day ,changed date and -f argument value will be sent to one java class. ... (13 Replies)
Discussion started by: karthinvk
13 Replies

7. Shell Programming and Scripting

convert date format YYYYMMDD to MM/DD/YYYY

In my shell script i have a variable which stores date in the format of YYYYMMDD. Is there any way to format this value to MM/DD/YYYY. Thanks. (8 Replies)
Discussion started by: nasirgondal
8 Replies

8. UNIX for Dummies Questions & Answers

Format date from MM/DD/YYYY to YYYYMMDD

I have a file with some date columns in MM/DD/YYYY format: SMPBR|DUP-DO NOT USE|NEW YORK||16105|BA5270715|6/6/2007 |MWERNER|109||||JOHN||SMITH|MD|72211118||||||74559|21 WILMINGTON RD||D|11/6/2003|SL# MD CONTACT-LIZ RICHARDS|||0|Y|N||1411458| And I want to convert the date format to: ... (5 Replies)
Discussion started by: ChicagoBlues
5 Replies

9. HP-UX

How can i get the yesterday's date in YYYYMMDD format

How can i get the yesterday's date in YYYYMMDD format??? (4 Replies)
Discussion started by: prasadsr
4 Replies

10. UNIX for Dummies Questions & Answers

get yesterday date in yyyymmdd format

I would like to know how I could get a yesterday date in yyyymmdd e.g. today is 20011109, and I would like to get 20011108. Thank you!:confused: (2 Replies)
Discussion started by: hk_newbie
2 Replies
Login or Register to Ask a Question