Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
google site



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Powered by Powered by Google
 
Search this Thread
  #1  
Old 07-15-2005
Registered User
 

Join Date: Jul 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
get yesterday in yyyymmdd format

how can i get yesterday in yyyymmdd format?
Sponsored Links
  #2  
Old 07-15-2005
Registered User
 

Join Date: Jun 2005
Location: Ireland
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
with gnu date you can do:
Code:
date --date="-1 days" +%Y%m%d


Last edited by Yogesh Sawant; 06-01-2010 at 09:03 AM.. Reason: added code tags
  #3  
Old 07-15-2005
RishiPahuja's Avatar
Registered User
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thanks: 0
Thanked 0 Times in 0 Posts
Wink

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
blowtorch's Avatar
AFK
 

Join Date: Dec 2004
Location: Singapore
Posts: 2,351
Thanks: 0
Thanked 1 Time in 1 Post
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
Registered User
 

Join Date: Jul 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
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
Technorati Master
 

Join Date: Mar 2005
Location: message delivery protocol
Posts: 3,072
Thanks: 6
Thanked 8 Times in 8 Posts

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
Registered User
 

Join Date: Jul 2005
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
My env is Japanese, so this happened.
Thank you for modification!
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
convert date format YYYYMMDD to MM/DD/YYYY nasirgondal Shell Programming and Scripting 8 04-08-2009 10:56 AM
Yesterday in i.e. May 09 and 05/09 format Daniel Gate Shell Programming and Scripting 3 05-20-2008 04:59 PM
How can i get the yesterday's date in YYYYMMDD format prasadsr HP-UX 4 01-19-2007 08:52 AM
Date increment in the format "YYYYMMDD" ganapati Shell Programming and Scripting 4 08-02-2006 11:45 AM
get yesterday date in yyyymmdd format hk_newbie UNIX for Dummies Questions & Answers 2 12-14-2001 02:32 PM



All times are GMT -4. The time now is 12:11 AM.