Urgent for date increment

hi all,

i would like to increment the date variable i am using

for((i=20190731;i<=20190801;i++))
do
done

after 20190731 it should be 20190801 but this taking as 20190732,20190733....
kindly help me to solve this

Hi,

It is doing what it is meant to be doing, you are incrementing a number and not a date. Consequently it will cycle through all the way to 20190800 before you get the correct entry - this would be fine if there were 100 day months and 100 months to the year but we aren't fully decimalised yet.

Here is some code that you could probably plugin with the minimum of modification.

DATE=2018-06-06

for i in {0..100}
do
   NEXT_DAY=$(date +%m-%d-%Y -d "$DATE + $i day")
   echo "$NEXT_DAY"
done

Regards

Gull04

If you want to reduce the number of date executions, try also (given your shell offers "process substitution", and date has the -f option):

$ DAYS=20
$ STARTDT=$(date +%s)
$ ENDDT=$((STARTDT + 86400 * DAYS))
 $ for ((STARTDT; STARTDT<ENDDT; STARTDT+=86400)); do echo @$STARTDT; done > >(date -f-)

EDIT: or even

for ((; (STARTDT+=86400)<ENDDT;  )); do echo @$STARTDT; done > >(date -f-)
1 Like

Hi.

If you are going to be dealing with dates frequently, you might want to look at a package called dateutils . It is available on several platforms. I use Debian, but it's available even on Apple/macOS via brew . Here is a demonstration, note that the key command is simply dateutils.deq :

#!/usr/bin/env bash

# @(#) s4       Demonstrate date sequence, dseq (part of dateutils).

# Infrastructure.
pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C dateutils.dseq specimen

rm -f f1
pl " Starting, ending dates:"
# date1="2012-01-01"
# date2="2012-12-31"
date1="2019-07-31"
date2="2019-10-01"
pe " $date1, $date2"

pl " Results, end-points:"
dateutils.dseq -f "%d-%m-%Y" "$date1" 1 "$date2" > f1
specimen 5:5:5 f1
rm -f f1

exit 0

producing:

$ ./s4

Environment: LC_ALL = en_US.utf8, LANG = en_US.utf8
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 3.16.0-7-amd64, x86_64
Distribution        : Debian 8.11 (jessie) 
bash GNU bash 4.3.30
dateutils.dseq dseq 0.3.1
specimen (local) 1.17

-----
 Starting, ending dates:
 2019-07-31, 2019-10-01

-----
 Results, end-points:
Edges: 5:5:5 of 63 lines in file "f1"
31-07-2019
01-08-2019
02-08-2019
03-08-2019
04-08-2019
   ---
29-08-2019
30-08-2019
31-08-2019
01-09-2019
02-09-2019
   ---
27-09-2019
28-09-2019
29-09-2019
30-09-2019
01-10-2019

Additional details:

dateutils.dseq  Generate a sequence of date/times from FIRST to LAST, ... (man)
Path    : /usr/bin/dateutils.dseq
Package : dateutils
Home    : http://www.fresse.org/dateutils
Version : 0.3.1
Type    : ELF 64-bit LSB shared object, x86-64, version 1 ( ...)
Help    : probably available with -h,--help
Home    : https://github.com/hroptatyr/dateutils (doc)

Best wishes ... cheers, drl