get yesterday in yyyymmdd format


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting get yesterday in yyyymmdd format
# 1  
Old 07-15-2005
get yesterday in yyyymmdd format

how can i get yesterday in yyyymmdd format? Smilie
# 2  
Old 07-15-2005
with gnu date you can do:
Code:
date --date="-1 days" +%Y%m%d


Last edited by Yogesh Sawant; 06-01-2010 at 10:03 AM.. Reason: added code tags
# 3  
Old 07-15-2005
Hammer & Screwdriver

if you have issue with the above method say you dont have gnu date... try connecting to oracle and get date from there...as there is no direct mechanism to calculate date.

something like this ...

Code:
yesterday=`sqlplus user/password << EOF
Select to_char(sysdate-1,'YYYYMMDD') from dual
Exit
EOF`

# 4  
Old 07-15-2005
I wrote this script to convert julian date into dd-mm-yyyy format. You could use this to get yesterday's date. You just have to get today's julian date (get this using `date +%j`) and subtract 1 from it. This will get you yesterday's julian date. Give this as the second argument to the script. The first argument is the present year in the YYYY format.

Note: I have changed the output so that it prints YYYYmmdd.

[edit]
Also this script will blindly convert whatever the julian date that is given to it. No error checking is done - so if you give the julian date as 0, the output you get for this year is 20050100
[/edit]

Code:
#!/bin/sh

check_done() {
        if [ $month -eq 1 -o $month -eq 3 -o $month -eq 5 -o $month -eq 7 -o $mo
nth -eq 8 -o $month -eq 10 -o $month -eq 12 ]
        then
                daysofmth=31
        elif [ $month -eq 2 ]
        then
                if [ `expr $year % 100` -eq 0 -a `expr $year % 400` -eq 0 ]
                then
                        daysofmth=29
                elif [ `expr $year % 100` -ne 0 -a `expr $year % 4` -eq 0 ]
                then
                        daysofmth=29
                else
                        daysofmth=28
                fi
        elif [ $month -eq 4 -o $month -eq 6 -o $month -eq 9 -o $month -eq 11 ]
        then
                daysofmth=30
        fi
        julday=`expr $julday - $daysofmth`
        if [ $julday -lt 0 ]
        then
                done=1
                julday=`expr $daysofmth + $julday`
        elif [ $julday -eq 0 ]
        then
                done=1
                julday=$daysofmth
        fi
}
#########  main script starts here
if [ $# -ne 2 ]
then
        echo "Usage: fromjul <yyyy> <julian day>"
        exit 1
fi

year=$1
julday=$2

month=0
done=0

while [ $done -ne 1 ]
do
        month=`expr $month + 1`
        check_done
done

printf "%.4d%.2d%.2d\n" $year $month $julday

# 5  
Old 07-15-2005
This does works exept for the 1st day of the month.
Code:
date | awk '{printf"%4d%2d%2d\n",$6,$2,($3-1)}' | sed 's/ /0/g'

# 6  
Old 07-16-2005
Code:
date | awk '{printf"%4d%2d%2d\n",$6,$2,($3-1)}' | sed 's/ /0/g'

i dont think the above one would work... the second parameter ($2) is displayed as such for month .. which is string format (ex: jul) but required output is in numeric format (07 - for jul)


here is the modification of the aboe one:

date '+%y:%m:%d' | awk -F":" '{printf"20%2d%2d%2d\n",$1,$2,($3-1)}' | sed 's/ /0/g'
# 7  
Old 07-16-2005
My env is Japanese, so this happened.
Thank you for modification!
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