INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

How to add 15 mins to the date format mentioned below

How to add 15 mins to the date format mentioned below

How to add 15 mins to the date format mentioned below

(OP)
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.

RE: How to add 15 mins to the date format mentioned below

Quote:

If I pass start date

Pass it to what???

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum

RE: How to add 15 mins to the date format mentioned below

(OP)
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

RE: How to add 15 mins to the date format mentioned below

how about a loop that counts to 4 and adds 1500 each time?

RE: How to add 15 mins to the date format mentioned below

Quote (pingat)

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 

RE: How to add 15 mins to the date format mentioned below

@ 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 # 

RE: How to add 15 mins to the date format mentioned below

(OP)
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&

RE: How to add 15 mins to the date format mentioned below

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 

RE: How to add 15 mins to the date format mentioned below

(OP)
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.

RE: How to add 15 mins to the date format mentioned below

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:
http://www.tutorialspoint.com/index.htm

Linux Man Pages:
http://www.linux.com/linux-man-pages

Scripting:
http://tldp.org/LDP/abs/html
String Operations:
Time/Date Commands:

... and don't forget Google.

RE: How to add 15 mins to the date format mentioned below

(OP)
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.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close