Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add 15 mins to the date format mentioned below 1

Status
Not open for further replies.

pplearnunix46

Programmer
Jan 15, 2015
6
US
Hi Friends,

I have a requirement like I need to add 15 mins to particular date format in a loop.

For Ex:
If I pass start date as 20150110000000 and end date as 20150111001500, I should get all the dates as mentioned below adding 15 mins to the date.

20150110000000(Jan 10,2015 00:00:00)
20150110001500(Jan 10,2015 00:15:00)
20150110003000(Jan 10,2015 00:30:00)
20150110004500(Jan 10,2015 00:45:00)
20150110010000(Jan 10,2015 01:00:00)
20150110011500(Jan 10,2015 01:15:00)
20150110013000(Jan 10,2015 01:30:00)
20150110014500(Jan 10,2015 01:45:00)
.
.
.
.
.
.
.
20150110234500(Jan 10,2015 23:45:00)
20150111000000(Jan 11,2015 00:00:00)
20150111001500(Jan 11,2015 00:15:00)

Can anyone please tell me how can I do it?

Thank you.
 
I am passing these dates as parameters to shell script. When i pass start date and end date, it should get me all the dates and those dates will be in turn passed to another shell script one by one. So I just need to know how can I get all the dates as mentioned with 15 mins difference. Is there any command which can help in addition of 15 mins
 
how about a loop that counts to 4 and adds 1500 each time?
 
pingat said:
how about a loop that counts to 4 and adds 1500 each time?

You can't do simple decimal math when there are only 60 seconds in a minute , 60 minutes in an hour, and 24 hours in a day.

It kind of works when the starting value ends with "000000". But even that is wrong since after adding "1500" four times you get "006000" which is an invalid time. That should roll over to "010000".

This gets close...

Code:
#!/bin/ksh

DATE=20150116

typeset -RZ2 HOUR MIN SEC HOUR

SEC=0

for HOUR in 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
do
        for MIN in 0 15 30 45
        do
                print "${DATE}${HOUR}${MIN}${SEC}"
        done
done

 
@ pplearnunix46
Just change the output to print fld1. Like so...
Code:
#!/bin/bash
# dates2.sh


dat=${1:0:8}
tim=${1:(-6):4}

fld1=$(date -d "$dat $tim" +'%Y%m%d%H%M%S')
printf "%s\n" $fld1

while [ $fld1 -lt $2 ]
do
    fld1=$(date -d "$dat $tim + 15 minutes" +'%Y%m%d%H%M%S')
    printf "%s\n" $fld1

    dat=${fld1:0:8}
    tim=$(date -d "$tim + 15 minutes" +'%H%M')
done

# eof #
 
Thanks Dioid :)

Now when I have got the dates. I want to pass this in another shell script , it should take each date as an argument and run and pass next date until the dates in the list are done.

For Ex:

nohup Shellscript 20150110000000 &
once this completes
nohup Shellscript 20150110001500 &
once this completes
nohup Shellscript 20150110003000 &
.
.
.
.
nohup Shellscript 20150111001500&
 
I have no idea what I'm doing here. You could try substituting the printf cmd with the nohup call in 2 places...
Code:
    # printf "%s\n" $fld1
    nohup ./prox.sh $fld1 > ./prox.log 2>&1 </dev/null &
    wait
 
Hi Dioid,

Thank u so much. I am new to unix shell scripting and I did whatever you told and it worked. I got the exact output that I was expecting :)

One more request for u, can u pls explain each line in script if u r ok with that. Sorry I am new so I am not getting it.

Thanks.
 
I don't mind helping with scripts, but, I'm not good at teaching or writing books. There are better sources than me to learn from. Starting with the command line, there is a lot of help and documentation at hand.

@commandline
Type:
help help
man man
info <command>

Tutorials:
[URL unfurl="true"]http://www.tutorialspoint.com/index.htm[/url]

Linux Man Pages:
[URL unfurl="true"]http://www.linux.com/linux-man-pages[/url]

Scripting:
[URL unfurl="true"]http://tldp.org/LDP/abs/html[/url]
String Operations:
Time/Date Commands:

... and don't forget Google.
 
Thank you. I will go through the documentation. but one last q, whats the logic behind below 2 lines.

dat=${1:0:8}
tim=${1:(-6):4}

Thanks.
 
whats the logic behind
man bash

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top