Format the Date output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Format the Date output
# 8  
Old 07-08-2010
There is an issue in the design of the original script where the wrong months are tested. It is most noticable for say the first of March 2010 (100301) when it would come out with yesterday as 31st March 2010 (100331) .

Working on the same idea but correcting the logic errors, I ended up with this script. Because we are working with a 2-digit year it stops working correctly after 31 December 2020 because year 2100 is not a leap year. The leading zeros issue is tackled with a "printf" statement.

Code:
#!/bin/sh
#       Display today and derive yesterday date YYMMDD. 
#       Works between 02/01/00 - 31/12/20
#
# Today
YY="`date +'%y'`"
MM="`date +'%m'`"
DD="`date +'%d'`"
TODAY="${YY}${MM}${DD}"
#
case "$MM" in
"01"|"02"|"04"|"06"|"08"|"09"|"11") # Last month was a 31 day month
        LASTDAY="31"    # Last day of last month
        ;;
"05"|"07"|"10"|"12")    # Last month was a 30 day month
        LASTDAY="30"    # Last day of last month
        ;;
"03")   # Last month was a 28/29 day month
        # Leap year adjustment
        # (does not work for year 2100 which is not a leap year)
        LEAP=`expr ${YY} % 4`
        if [ ${LEAP} -eq 0 ]
        then
                LASTDAY="29"
        else
                LASTDAY="28"
        fi
        ;;
*)
        echo "Invalid month: ${MM}"
        ;;
esac
#
if [ "${DD}" = "01" ]
then
        DD2="${LASTDAY}"        # Last day of last month
        if [ "${MM}" = "01" ]
        then
                MM2="12"
                YY2=`expr ${YY} - 1`
        else
                MM2=`expr ${MM} - 1`
                YY2="${YY}"
        fi
else
        DD2=`expr ${DD} - 1`
        MM2="${MM}"
        YY2="${YY}"
fi
#
YESTERDAY=`printf '%.2d%.2d%.2d' $YY2 $MM2 $DD2`
echo "${TODAY}"
echo "${YESTERDAY}"


Last edited by methyl; 07-09-2010 at 07:08 AM.. Reason: Correct wrong result value from original script (actually March).
# 9  
Old 07-08-2010
An old thread

I posted into the following once before.
https://www.unix.com/unix-dummies-que...-date-7-a.html

a) determines a date
b) calculation on the date
c) convert date (from epoch time) to YYMMDD format

Perhaps this will give you some ideas?
# 10  
Old 07-08-2010
@joeyg
Somewhere in a parallel thread O/P mentions he has "Solaris" - which doesn't come with "date -d".
# 11  
Old 07-09-2010
Thnaks Pravin , It helped with a little modification ...

Thanks a lot Smilie
# 12  
Old 07-09-2010
@ultimax
To reiterate. The script posted in #6 and modified by pravin27 in #7 contains basic logic errors. The pravin27 issue contains a syntax error (unmatched "else").
Whatever, the result is always wrong on the first of the month (which takes up most of the code) because it does not decrement the month or year and because the displacement values are based on the number of days in this month not the number of days in last month.

My version builds on the idea but corrects the errors.

When you have a final working version, please post it to close your two threads.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Date: invalid date trying to set Linux date in specific format

i try to set linux date & time in specific format but it keep giving me error Example : date "+%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" or date +"%d-%m-%C%y %H:%M:%S" -d "19-01-2017 00:05:01" keep giving me this error : date: invalid date ‘19-01-2017 00:05:01' Please use CODE tags... (7 Replies)
Discussion started by: umen
7 Replies

2. UNIX for Dummies Questions & Answers

Rename all Files in a UNIX Directory from one date format to another date format

Hi Unix Gurus, I would like to rename several files in a Unix Directory . The filenames can have more than 1 underscore ( _ ) and the last underscore is always followed by a date in the format mmddyyyy. The Extension of the files can be .txt or .pdf or .xls etc and is case insensitive ie... (1 Reply)
Discussion started by: pchegoor
1 Replies

3. Shell Programming and Scripting

finding date numeral from file and check the validity of date format

hi there I have file names in different format as below triss_20111117_fxcb.csv triss_fxcb_20111117.csv xpnl_hypo_reu_miplvdone_11172011.csv xpnl_hypo_reu_miplvdone_11-17-2011.csv xpnl_hypo_reu_miplvdone_20111117.csv xpnl_hypo_reu_miplvdone_20111117xfb.csv... (10 Replies)
Discussion started by: manas_ranjan
10 Replies

4. UNIX for Dummies Questions & Answers

Shell Scripts - shows today’s date and time in a better format than ‘date’ (Uses positional paramete

Hello, I am trying to show today's date and time in a better format than ‘date' (Using positional parameters). I found a command mktime and am wondering if this is the best command to use or will this also show me the time elapse since 1/30/70? Any help would be greatly appreciated, Thanks... (3 Replies)
Discussion started by: citizencro
3 Replies

5. Shell Programming and Scripting

Format output from the file to extract "date" section

Hello Team , I have to extract date section from the below file output. The output of the file is as shown below. I have to extract the "" this section from the above output of the file. can anyone please let me know how can we acheive this? (4 Replies)
Discussion started by: coolguyamy
4 Replies

6. Shell Programming and Scripting

How to increment a user defined date value in the DATE format itself using shell script?

I need to increment a date value through shell script. Input value consist of start date and end date in DATE format of unix. For eg. I need increment a date value of 1/1/09 to 31/12/09 i.e for a whole yr. The output must look like 1/1/09 2/2/09 . . . 31/1/09 . . 1/2/09 . 28/2/09... (1 Reply)
Discussion started by: sunil087
1 Replies

7. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

8. Shell Programming and Scripting

convert date format to mysql date format in log file

I have a comma delimited log file which has the date as MM/DD/YY in the 2nd column, and HH:MM:SS in the 3rd column. I need to change the date format to YYYY-MM-DD and merge it with the the time HH:MM:SS. How will I got about this? Sample input 02/27/09,23:52:31 02/27/09,23:52:52... (3 Replies)
Discussion started by: hazno
3 Replies

9. UNIX for Advanced & Expert Users

date issue-find prevoius date in a patricular format

Hi , I have written a shell script that takes the current date on the server and stores it in a file. echo get /usr/home/data-`date '+%Y%d'`.xml> /usr/local/sandeep/GetFILE.ini I call this GetFILE.ini file from an sftp program to fetch a file from /usr/home/ as location. The file is in... (3 Replies)
Discussion started by: bsandeep_80
3 Replies

10. Shell Programming and Scripting

convert mmddyy date format to ccyyddd format??

hi, for reading a cobol indexed file i need to convert "mmddyy" date format to "ccyyddd" format. i checked the datecalc and other scripts but couldnt modify them to cater to my need:(... The datecalc gives an output which i believe is the total days till that date, but i want to convert it... (2 Replies)
Discussion started by: Bhups
2 Replies
Login or Register to Ask a Question