Urgent for date increment


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Urgent for date increment
# 1  
Old 10-22-2019
Urgent for date increment

hi all,

i would like to increment the date variable i am using
Code:
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

Last edited by Peasant; 10-22-2019 at 05:36 AM.. Reason: Added code tags to possible code..
# 2  
Old 10-22-2019
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.

Code:
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

Last edited by gull04; 10-22-2019 at 07:41 AM.. Reason: Thoght I'd invented a new word - but just got the spelling very wrong on an old word!
# 3  
Old 10-22-2019
If you want to reduce the number of date executions, try also (given your shell offers "process substitution", and date has the -f option):

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

EDIT: or even
Code:
for ((; (STARTDT+=86400)<ENDDT;  )); do echo @$STARTDT; done > >(date -f-)


Last edited by RudiC; 10-22-2019 at 08:13 AM..
This User Gave Thanks to RudiC For This Post:
# 4  
Old 10-22-2019
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:
Code:
#!/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:
Code:
$ ./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:
Code:
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Increment date variable

hey guys, I need to incerement the date variable for instance echo `date '+%F %H:%M:00'` this produces 2014-08-02 20:05:00 -I will grant this to : $Datehour and need to assign 1 hr from now to $Datelasthour -the script time will be used to talk to DB system information. however... (4 Replies)
Discussion started by: mo_VERTICASQL
4 Replies

2. Shell Programming and Scripting

Date increment logic

Hi all, I need to increment date at run time. Example: I need to write a shell script with two parameters. 1. country code like (US,UK, IND.....) 2. Date range from_date to to_date (20070101 to 20070331) I need to run shell script like this country_info.sh US 20070101 20070331 ... (3 Replies)
Discussion started by: pmreddy
3 Replies

3. Shell Programming and Scripting

how to update date part with new increment date time

hi experts, my requirement is like this i need to develop a shell script to update date part with new incremental date time in file some 'X' which is kept at some server location incrementing every two hours.as i am new to this scripting i need support from u people,thanx in advance (1 Reply)
Discussion started by: amanmro
1 Replies

4. Shell Programming and Scripting

Date increment

hi Friends, Today_Dt=`date "+%Y-%m-%d"` So the Today date is 2010-05-03 I have a file which has date values as below 2010-04-27 2010-04-02 2010-04-18 2010-04-28 2010-04-29 .. (1 Reply)
Discussion started by: Gopal_Engg
1 Replies

5. Shell Programming and Scripting

Increment in date

Hi, I have a variable lets say DATA_DATE. I have to pass some value to this variable in YYYYMMDD format. lets say today I have passed this variable as : DATA_DATE=20100107 Then pls help me how to calculate another variable DATA_DATE1 (which is DATA_DATE+1). The code should work... (3 Replies)
Discussion started by: 46019
3 Replies

6. Shell Programming and Scripting

Increment date in script

Hi, Iam new to scripting language.:o can someone help me out solving this thread?hopingly ya....:) I want to write a script which connects to db and searches the count in a table which has date column and id column. If the count is not equal to 0 then it should increment the date with the one... (4 Replies)
Discussion started by: jyothi_wipro
4 Replies

7. 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

8. Shell Programming and Scripting

Can we increment or decrement a date value?

export a=`date` a=`expr $a + 1` Is it possible? if not how can i increment or decrement a date variable? (2 Replies)
Discussion started by: arghya_owen
2 Replies

9. Shell Programming and Scripting

Shell Script To increment Date*HElp***

Hi, I want to increment date, using Shell Script, a loop to add one day after 24hrs. :confused: Cheers Kunal (1 Reply)
Discussion started by: niceboykunal123
1 Replies

10. Shell Programming and Scripting

Increment date in 'for' loop?

Hi Guys, My first post..:) Right...I want to move existing files (with some date in their name) currently in $mainftp, to $mainfolder/$foldate/system1. I'd like to be able to increment date in the for loop? Is this possible or should I use a different technique. The script return the... (4 Replies)
Discussion started by: SunnyK
4 Replies
Login or Register to Ask a Question