Get the no of hours between days


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the no of hours between days
# 1  
Old 10-18-2013
Get the no of hours between days

Hi,

i have a date 1- 2013101511
date2 -2013101812

need toget the no of hours between them,can any one tellme the logic.
# 2  
Old 10-18-2013
Refer thread: Date Arithmetic
# 3  
Old 10-18-2013
I'm supposing that the latest two digits rapresents the hour.

In bash:
Code:
date1='2013101511'
date2='2013101812'
ts_date1=$(date --date="${date1:0:8} ${date1:8}" '+%s')
ts_date2=$(date --date="${date2:0:8} ${date2:8}" '+%s')
let diff_hours=(ts_date2-ts_date1)/3600
echo $diff_hours

Emanuele
# 4  
Old 10-18-2013
Note that --date only works on GNU date, usually not present anywhere but GNU/Linux. For date math that works anywhere, you'll likely need perl.

So, what's your system?
# 5  
Old 10-18-2013
yes its GNU/Linux

---------- Post updated at 02:52 PM ---------- Previous update was at 02:50 PM ----------

yes thts correct the last two digits are hours.
# 6  
Old 10-18-2013
Another approach using ksh93 builtin printf %T formatting option:
Code:
#!/bin/ksh93

date1="2013101511"
date2="2013101812"

SDATE=$( printf "%(%s)T" "${date1:0:8} ${date1:8:2}:00:00" )
EDATE=$( printf "%(%s)T" "${date2:0:8} ${date2:8:2}:00:00" )

printf "%d\n" "$(( ( $EDATE - $SDATE ) / 3600 ))"

# 7  
Old 10-18-2013
thanks yoda..it worked as desired.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to convert days hours minutes seconds to minutes?

Hi, please help with below time conversion to minutes. one column values: 2 minutes 16 seconds 420 msec 43 seconds 750 msec 0 days 3 hours 29 minutes 58 seconds 480 msec 11 seconds 150 msec I need output in minutes(total elapsed time in minutes) (2 Replies)
Discussion started by: ramu.badugula
2 Replies

2. UNIX for Beginners Questions & Answers

How to find a file that's modified more than 2 days ago but less than 5 days ago?

How to find a file that's modified more than 2 days ago but was modified less than 5 days ago by use of any Linux utility ? (4 Replies)
Discussion started by: abdulbadii
4 Replies

3. Shell Programming and Scripting

Can anyone help me to print UNIX epoch time to days,hours,min,sec ?

I have unix epoch time 1441678454803, Can you please help me to print this time in below format ? DAY,HOUR,MIN,SEC Appreciate your help!!! Thanks, Prince (7 Replies)
Discussion started by: prince1987
7 Replies

4. Shell Programming and Scripting

Working out days of the week and processing file in 3 working days

Hi guys i need advice on the approach to this one...... I have a file say called Thisfile.20130524.txt i need to work out from the date 20130524 what day of the week that was and then process the file in 3 working days. (so not counting saturday or sunday....(will not worry about bank... (2 Replies)
Discussion started by: twinion
2 Replies

5. Shell Programming and Scripting

Cron job running for some days and is not running for some days

Hi.. i have written a shell script and made this script to run on every day night 11: 55 pm using a cron job. This cron job running for some days and is not running for some day. but i need this script to run every day night. Please help me. Here is the cron tab entries, 55 23 * * *... (1 Reply)
Discussion started by: vidhyaS
1 Replies

6. Shell Programming and Scripting

date for two days or 3 days ago

i need a script that can tell me the date 2 days ago or 3 days ago. please help (7 Replies)
Discussion started by: tomjones
7 Replies

7. Shell Programming and Scripting

how to list files between last 6 hours to 3 hours

Hi Frens, I want to list some files from a directory, which contains "DONE" in their name, i am receiving files every minute. In this i want to list all the files which are newer than 6 hours but older than 3 hours, of current time i dont want my list to contain the latest files which are ... (4 Replies)
Discussion started by: Prat007
4 Replies

8. Post Here to Contact Site Administrators and Moderators

Have we just had a rollback of a few hours?

Have we just had a rollback of a few hours? (1 Reply)
Discussion started by: porter
1 Replies

9. Shell Programming and Scripting

Add data in days:hours:min:sec format

I want to add these no. these are in the format of days:hours:minutes:sec I want result in this format only 0:00:04:59 0:00:00:12 0:00:00:28 0:00:00:03 0:01:29:35 0:00:00:19 0:01:05:21 Is any body ca help me????? To get This.. Thanks Nishant (1 Reply)
Discussion started by: krishna_sicsr
1 Replies

10. Shell Programming and Scripting

ls latest 4 days or specify days of files in the directory

Hi, I would like to list latest 2 days, 3 days or 4 days,etc of files in the directory... how? is it using ls? (3 Replies)
Discussion started by: happyv
3 Replies
Login or Register to Ask a Question