-   Linux - General (
-   -   elapsed time calculation from two date strings - shell script (

hisunday 03-28-2012 06:52 PM

elapsed time calculation from two date strings - shell script
Looking for help.

Have got two variables defined as below. Actually the values are date strings (yyyymmddHHMMSS year month day 24hour min second)

Looking for some logic (that could be used in shell script) to find the difference of x2 and x1 in terms of seconds (or minutes). Thanks.

unSpawn 03-28-2012 07:00 PM

Easiest way IMHO would be to not use time stamps but epoch instead as that's easier to work with. Note the 'date' command can convert human readable dates to epoch and back in about every format you can think of.

hisunday 03-28-2012 07:25 PM

Thanks for your response. Actually in our case the timestamp values (x1 and x2) are already stored in flat files for thousands of records and I need to calculate the elapsed time for these records. We get this flat file from another interface system where we don't have much control. In my original post I gave a simplified requirement by defining two variables x1 and x2. Thanks.

unSpawn 03-28-2012 09:12 PM

This may explain on stdout what I mean:

INITDATE="${x1:0:4}-${x1:4:2}-${x1:6:2} ${x1:8:2}:${x1:10:2}:${x1:12:2}"
echo x1="$(eval echo env TZ=UTC date --date=\"${INITDATE}\" +'%s')"

hisunday 03-28-2012 11:26 PM

That's great. It resolved the issue. Thanks a lot.

onebuck 03-29-2012 08:11 AM

Moderator response
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate of ;

All times are GMT -5. The time now is 12:04 PM.