Sponsored Content
Top Forums UNIX for Dummies Questions & Answers Display dates within a given date range Post 302281996 by frozensmilz on Friday 30th of January 2009 01:56:37 AM
Old 01-30-2009
Solution for generating dates between given dates

I believe this shell script will solve the problem. I was hoping for any UNIX built in functions to do the job. Because there is no point in reinventing the wheel. Please feel free to cut short my algorithm for a better code. And if there is any built in functions please inform me.

Currently the output is in DDMMYY format. if you want it in DDMMYYYY format just edit the below line in the code & remove the 'cut' section.

year=`echo $3 | cut -c3-4`

Thanks for all the support from the UNIX forum members
1. The script cecho will echo the output in color so if you dont have cecho replace it by echo. I believe i got cecho from someone in UNIX Forum.
2. The anflg is my script to check whether the date contains alphabetic or alphanumeric characters. You can comment the calls to validation functions to bypass calls to external scripts, but make sure the input is in correct format.
3. The real algorithm lies in the last for loop, Generate Dates . This for loop will take the dates from unix cal function and generates the date

The name for the source code in gendt and run them as

gendt <From date> <To date>

Bye. SmilieSmilie

HTML Code:
# gendt - DATE GENERATOR WITHIN THE GIVEN RANGE
#
# USAGE
#
# gendt <From> <To>
#
# Note: 1. The date must be in DDMMYYYY format
#    2. The <From> date must be smaller than <To> date
#        Ex: dt 30012009 28022009
#    3. The output is in DDMMYY format
#     4. Invalid month days like 300209 (feb has 28 days) & 310408 
#       are corrected automatically by the script
#
# MAINTENANCE HISTORY 
# DATE        AUTHOR AND DETAILS
# 30-01-09    BAS     TR - ORIGINAL 
#
#----------------------------------------------------------------------------79 

frmDate=$1
toDate=$2

# Basic data check
dateChk1() {
    
    len=`echo $1 | wc -c`
    len=`expr $len - 1`

    if [ $len -ne 8 ]; then
        cecho green "Invalid date length (DDMMYYYY)."
        exit
    fi
    
    alpNumFlg=`anflg "$1" "AN"`
    alpFlg=`anflg "$1" "AT"`
    
    finFlg=`expr $alpNumFlg + $alpFlg`
    if [ "$finFlg" != "0" ]; then
        cecho green "$1 is not a number"
        exit
    fi
        
}


dateChk1 "$frmDate"
dateChk1 "$toDate"

# Basic date validation
dateChk2 () {

    case $2 in
        "M")                    
            if [ $1 -le  12 ]; then                        
                if [ $1 -le 0 ]; then                                    
                    cecho green "$1 is an invalid month (1-12)"
                    exit
                fi
            else
                cecho green "$1 is an invalid month (1-12)"
                exit
            fi
            ;;
        "D")
            if [ $1 -le 31 ]; then
                if [ $1 -le 0 ]; then
                    cecho green "Day value $1 must be within (1-31)"
                    exit
                fi
            else
                cecho green "Day value $1 must be within (1-31)"
                exit
                
            fi
            ;;

        "Y")
            if [ $1 -le 0 ]; then
                cecho green "$1 is an invalid year."
                exit
            fi
            ;;
    esac
}

frmDtDay=`echo $frmDate | cut -c1-2`
frmDtMon=`echo $frmDate | cut -c3-4 | tr -d 0`
frmDtYer=`echo $frmDate | cut -c5-8`

toDtDay=`echo $toDate | cut -c1-2`
toDtMon=`echo $toDate | cut -c3-4 | tr -d 0`
toDtYer=`echo $toDate | cut -c5-8`

dateChk2 "$frmDtDay" "D"
dateChk2 "$frmDtMon" "M"
dateChk2 "$frmDtYer" "Y"
dateChk2 "$toDtDay" "D"
dateChk2 "$toDtMon" "M"
dateChk2 "$toDtYer" "Y"

# Make any D & M day,month formats to DD,MM respectively
dateFill() {

    val="$1"    
    dtLen=`echo "$val" | wc -c`
    dtLen=`expr $dtLen - 1`
    if [ $dtLen -lt 2 ]; then
        val="0$1"    
    fi
    return $val
}

yerDiff=`expr $toDtYer - $frmDtYer`
if [ $yerDiff -lt 0 ]; then
    cecho green "Date $frmDate must be less than date $toDate."
    exit
fi

# Display the date in DDMMYY format
datDisp () {

    dateFill $1
    dayTmp=$val
    dateFill $2
    monTmp=$val
    year=`echo $3 | cut -c3-4`    
    echo "$dayTmp$monTmp$year"
}

# Generate dates
for y in $(seq $frmDtYer $toDtYer); do
    for m in $(seq 1 12); do        
        dtGenIni=`cal $m $y | grep -v [A-z] | tr -s ' ' '\n'`
        for d in $dtGenIni; do
                                
            day=$d
            # same year
            if [ $y -eq $toDtYer ]; then
                # same month
                if [ $m -eq $toDtMon ]; then
                    # same day
                    if [ $day -eq $toDtDay ]; then
                        # if all the above, print the same BAS-BAS-BAS day
                        datDisp $day $m $y
                    # different days
                    else                    
                        # print days between From & To Days
                        if [ $day -ge $frmDtDay ]; then
                            if [ $day -le $toDtDay ]; then
                                datDisp $day $m $y                                
                            fi
                        fi                    
                    fi
                # different months
                else
                    # print days between From & To Months                    
                    if [ $m -ge $frmDtMon ]; then                                
                        if [ $m -le $toDtMon ]; then
                            # if curent month is From or To, 
                            # remove dates outside the range                            
                            if [ $m -eq $frmDtMon ]; then
                                if [ $day -ge $frmDtDay ]; then                                    
                                    datDisp $day $m $y
                                fi
                            else
                                    datDisp $day $m $y
                            fi
                        fi
                    fi
                fi    
    
            # different year    
            else
                # print the days between the From & To Years
                if [ $y -ge $frmDtYer ]; then
                    if [ $y -le $toDtYer ]; then
                        # check for same From year
                        if [ $y -eq $frmDtYer ]; then
                            # check for same month
                            if [ $m -eq $frmDtMon ]; then
                                if [ $day -ge $frmDtDay ]; then
                                    datDisp $day $m $y
                                        
                                fi                                
                            fi
                        fi
                        # check for same To Year
                        if [ $y -eq $toDtYer ]; then
                            # check for same month
                            if [ $m -eq $toDtMon ]; then
                                datDisp $day $m $y
                            fi                    
                        else
                            # simply print other dates within the range
                            datDisp $day $m $y
                        fi                    
                    fi    
                fi
            fi                                    
        
        done
    done
done
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to display files that have been modifed between a given date range

Hi, I am new to Unix and was trying different ways of how to display the list of file names modified between a given date range in sorting order.I will get the fromdate and Todate from the browser, I need to display the list of all the file names that are modified between the given date... (1 Reply)
Discussion started by: prathima
1 Replies

2. Shell Programming and Scripting

display the file with in the date range

Hi All, I want a shell script which can display the file with in the date range. For Example I have 15 files with the following format abc_01-01-2009.txt to abc_15-01-2009.txt. Now I want to have the files between 4th of jan to 12th files. How can I acheive this. Advance... (1 Reply)
Discussion started by: fareed_do
1 Replies

3. Shell Programming and Scripting

Display the last five dates from the given date

Hi all, In Oracle we have got sysdate -1 to find the previous date. Is there any similar way to display date in unix shell scripting? Kindly help me to display the last five dates from the given date Thanks, Geetha (11 Replies)
Discussion started by: iamgeethuj
11 Replies

4. Shell Programming and Scripting

pull range of dates/times and put into new file

Need to pull from a range of dates/times (ex. 6:00 AM March 3 through 6:00 AM March 4) from a folder and put those file names in a new file to process later. Dates would not be hard dates but dates from the folder directory, how would I do that? (9 Replies)
Discussion started by: freddie999
9 Replies

5. Shell Programming and Scripting

Using 'date' to list a range of dates

Hi guys, I have been trying to create a list of dates from a certain range, ie. range from 01011950 to 31122000 But when my below code reaches certain dates, it comes up with a; 'date: invalid date 'yyyy-mm-dd -d 1day' Sofar I have come up with the following, slow and ugly; ... (4 Replies)
Discussion started by: TAPE
4 Replies

6. Emergency UNIX and Linux Support

show div on select - range of dates

Hi, I am sure this is simple, but I am breaking my head. I need 1 page with at the top a range of dates, 2002, 2003, 2004 etc If you select 2002 it will show the content of 1 div, if you select 2002 the content of another div. this is for showing announcements on a site, right now there... (1 Reply)
Discussion started by: lawstudent
1 Replies

7. Shell Programming and Scripting

display Range of date depend on user input php

Hi, i am very new to php Is it possible to display Range of date depend on user input day example: user input 2 day start from 28/4/12 it will add 2 day from date of input so display should look like this 28/4/12 to 30/4/12 then from 30/412 user add another 4 date so will... (0 Replies)
Discussion started by: guidely
0 Replies

8. Shell Programming and Scripting

Display data from a range of dates

I have a data in a file called SCHED which has 5 columns: sched no, date, time, place and remarks. The image is shown below. http://dl.dropbox.com/u/54949888/Screenshot%20from%202013-01-02%2002%3A42%3A25.png Now, I want to display only the schedules which fall under a certain date range which... (2 Replies)
Discussion started by: angilulu
2 Replies

9. Shell Programming and Scripting

Display lines of two date range from syslog file

Hi Guys, I want to display lines from Solaris syslog file but with 2 dates range. I have some similar solution (https://www.unix.com/shell-programming-scripting/39293-grep-log-file-between-2-dates-4.html) which works fine but as you know syslog has different date format (Jan 22) so this is not... (1 Reply)
Discussion started by: prashant2507198
1 Replies

10. Shell Programming and Scripting

Display dates between two dates

Hi All, I have 2 dates in mm/dd format. sdate=10/01 (October 01) edate=10/10 (October 10) I need the dates in between these 2 dates like below. 10/01 10/02 10/03 10/04 10/05 10/06 10/07 10/08 (1 Reply)
Discussion started by: jayadanabalan
1 Replies
All times are GMT -4. The time now is 02:50 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy