Date after 5 days from current date in YYYYMMDD format

Hello Experts,

How do i get date after 5 days from current date in YYYYMMDD format?

How do you compare date in YYYYMMDD format?

Thanks

You would have to write a script which takes in account for instance like today:
we are month=09 day=29
If you add 5 to 29, you are a bit out of day range... ( you would have to reset day (+4) and add 1 to month value...)

---------- Post updated at 17:50 ---------- Previous update was at 17:31 ----------

Some clues:

# with this date format month position is 99 to 9999 and day 9 to 99 so:

ant:/home/vbe $ IN5DAYS=$(expr $(date +%Y%m%d) + 105);echo $IN5DAYS 
20101034      # 34 days sep had 30 so:
ant:/home/vbe $ IN5DAYS=$(expr $IN5DAYS - 30);echo $IN5DAYS        
20101004
octant:/home/vbe $ 

If you have GNU date

date -d "5 days"

Waiting to see the script you will have to write! (nice algorithm...)

The following DIBOL program is a re hash of a program published in Computer World Newspaper in the early 1970's. The original was written in Fortran
It converts a date in the format yyyymmdd into the number days that have elapsed since Jan 1 1900.
It also converts a number representing the number of days since Jan 1 1900 to a date.

All arithmetic is integer, so 8/3 = 2

#cat xdatex.dbl
.SUBROUTINE XDATEX
DATEXREF,    A
RECORD DATEREC
           CLDRDATE, D8
               CLDRYEAR, D4 @CLDRDATE 
               CLDRMTH,  D2 @CLDRDATE +4    
               CLDRDAY , D2 @CLDRDATE +6
           DAYS1901, D10            
           DAYOFWK,  D1
           NAMOFDAY, A9      
           NAMOFMTH, A9      
RECORD 
        CALCYEAR , D10 
        CALCMTH ,D10
        CALCDAY , D10 
        DYYR , D10 
        DYWK , D10
        DYMO , D10 
        TEMPYEAR , D10 
        TEMPDAY , D10 
        LEAPYEAR , D10
        HOLDDATE , D8   
        M, A8
        DAYDATA, 7A9, 'SUNDAY   ','MONDAY   ','TUESDAY  ','WEDNESDAY'
&,'THURSDAY ','FRIDAY   ','SATURDAY '
        MTHDATA, 12A9, 'JANUARY  ','FEBRUARY ','MARCH   ','APRIL   ',
&'MAY     ','JUNE    ','JULY    ','AUGUST  ',
&'SEPTEMBER','OCTOBER  ','NOVEMBER ','DECEMBER '
PROC
SOJ,
DATEREC=DATEXREF
HOLDDATE=CLDRDATE
IF (DAYS1901 .NE. 0) GO TO CENTOCLD
LEAPYEAR=2
CALCYEAR= CLDRYEAR - 1900
CALCMTH= CLDRMTH
CALCDAY= CLDRDAY
TEMPYEAR= CALCYEAR/4
TEMPYEAR= TEMPYEAR * 4
IF (TEMPYEAR .EQ. CALCYEAR)  LEAPYEAR=1
DYYR= (CALCMTH * 275)/9 + CALCDAY - 30
IF (CALCMTH .GT. 2) DYYR= DYYR -LEAPYEAR
DAYS1901= CALCYEAR - 1
DAYS1901= (DAYS1901 * 1461) / 4 + DYYR
CALL    CENTOCLD
IF (CLDRDATE .NE. HOLDDATE)
BEGIN
DAYS1901= 0
CLDRDATE= 0
DAYOFWK = 0
NAMOFDAY=  
NAMOFMTH=
DATEXREF=DATEREC
RETURN
END
RETURN
CENTOCLD,
CALCYEAR=(DAYS1901/1461) 
CALCYEAR=(DAYS1901 - CALCYEAR + 364)/365
DYYR=((CALCYEAR - 1)*1461)/4
DYYR=DAYS1901-DYYR
LEAPYEAR= 2
        TEMPYEAR= CALCYEAR/4
        TEMPYEAR= TEMPYEAR * 4
IF (TEMPYEAR .EQ. CALCYEAR) LEAPYEAR=1
TEMPDAY= DYYR
TEMPYEAR= 61 - LEAPYEAR 
IF (TEMPDAY .GT. TEMPYEAR) TEMPDAY= TEMPDAY + LEAPYEAR
CALCMTH= (TEMPDAY * 9 + 269) / 275
DYMO= ((CALCMTH * 275) / 9) - 30
CALCDAY= TEMPDAY - DYMO
DYMO=CALCDAY
DYWK=DAYS1901 + 1
DYWK=DYWK-((DYWK/7)*7)+1
DAYOFWK=DYWK
CLDRYEAR = CALCYEAR + 1900
CLDRMTH= CALCMTH
CLDRDAY= CALCDAY
NAMOFDAY=  DAYDATA(DYWK)
NAMOFMTH=MTHDATA(CALCMTH)
DATEXREF=DATEREC
RETURN
#

I think this is what you're looking for.

date -d "5 days" +%Y%m%d

What type of comparison are you doing? If you just want to know which dates are before or after, a simple date1 > date2 (or vice versa) will work if the date is formatted in YYYYMMDD format. If you need to know specific amount of time between dates you might have to write a shell script. Let me know which you're trying to do and I'll to help out.

Basically, I would like to find last date of next month in YYYYMMDD format and want to find number of days between today and last date of next month.

Thanks

Here's one way to do it with Perl -

$
$
$ perl -MDateTime -le '$d = DateTime->now;
                       $start = $d->clone;
                       $d1 = $d->add(months=>1);
                       $end = DateTime->last_day_of_month(year=>$d1->year, month=>$d1->month);
                       print "The date today          = ", $start->ymd("");
                       print "Last date of next month = ", $end->ymd("");
                       print "Number of days left     = ", $start->delta_days($end)->delta_days;
                      '
The date today          = 20100930
Last date of next month = 20101031
Number of days left     = 31
$
$

tyler_durden

That sounds different than the original post. This probably isn't the most elegant way to do this, but it works and keeps the dependencies to a minimum. (Just GNU date)

#!/bin/bash

#Get last date
dt=$(($(date +%-m) + 2))
yr=$(date +%Y)
if [ $dt -gt 12 ]; then
        dt=$((1 + ($dt - 12)))
        yr=$(($yr + 1))
fi
value=$(date -d "$yr-$dt-01 -1 day" +%Y%m%d)

echo "Last day of next month is $value"

#Get days between

seconds1=$(date -d $value +%s)
seconds2=$(date +%s)
elapsed=$(($seconds1 - $seconds2))
secondsperday=86400
numdays=$(($elapsed / $secondsperday))
echo "Number of days between is $numdays"