<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to find the difference between dates only counting business hours? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116650#M30987</link>
    <description>&lt;P&gt;Sorry, it did not work.&lt;BR /&gt;
If start_time = 26/08/2014 3:23:29 PM and close_time=27/08/2014 12:15:18 PM , your query gives me total_time=20.86 hours, whereas i expect close_time = 4.52hours.&lt;BR /&gt;
Not sure if converting epoch time is culprit here.Or am i doing something wrong here?&lt;/P&gt;</description>
    <pubDate>Mon, 28 Sep 2020 17:33:06 GMT</pubDate>
    <dc:creator>karthikTIL</dc:creator>
    <dc:date>2020-09-28T17:33:06Z</dc:date>
    <item>
      <title>How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116644#M30981</link>
      <description>&lt;P&gt;HI,I have two fields A and B with time format as 1/07/2014 3:41:12 PM.&lt;BR /&gt;
e.g., if A is 1/07/2014 3:41:12 PM  and B is 15/07/2014 2:41:12 PM.&lt;BR /&gt;
To find difference i know we need to use,&lt;BR /&gt;
"eval epoch_A=strptime('Field A',"%d/%m/%Y %I:%M:%S %p") | eval epoch_B=strptime('Field B',"%d/%m/%Y %I:%M:%S %p") | eval diff=round(('epoch_B'-'epoch_A') / 3600)|table diff"&lt;/P&gt;

&lt;P&gt;Please let me know how to find difference in hours, taking into account only business hours i.e., from 9AM to 5PM and excluding Saturday,Sunday,Public Holiday.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:31:31 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116644#M30981</guid>
      <dc:creator>karthikTIL</dc:creator>
      <dc:date>2020-09-28T17:31:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116645#M30982</link>
      <description>&lt;P&gt;Hi karthikTIL,&lt;/P&gt;

&lt;P&gt;Each event has a field called  &lt;CODE&gt;date_hour&lt;/CODE&gt; which has the hour of the day as value, maybe this is of help in your case.&lt;BR /&gt;
Something like this just to give an example on how to use &lt;CODE&gt;date_hour&lt;/CODE&gt;: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;Your base search date_hour&amp;gt;08 OR date_hour&amp;lt;18 | your next cammond
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;UPDATE:&lt;/P&gt;

&lt;P&gt;Basically you can just add the &lt;CODE&gt;date_hour&lt;/CODE&gt; and &lt;CODE&gt;date_wday&lt;/CODE&gt; to the base search like this :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;source="file.csv" date_month=august date_hour&amp;gt;08 AND date_hour&amp;lt;18 NOT ( date_wday=Saturday OR date_wday=Sunday ) 
| bucket _time span=1d 
| eval epoch_A=strptime('start_TIME',"%d/%m/%Y %I:%M:%S %p") 
| eval epoch_B=strptime('close_TIME',"%d/%m/%Y %I:%M:%S %p") 
| eval total_TIME=(('epoch_B'-'epoch_A') / 3600)
| table start_TIME,close_TIME,total_TIME
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This way you will only get back results for this time range ( 9am to 5pm ) and only on weekdays Mo - Fr. You can decide if you need the &lt;CODE&gt;bucket&lt;/CODE&gt; or not, this will &lt;CODE&gt;group&lt;/CODE&gt; all events into per day slices, which will be used in &lt;CODE&gt;timechart&lt;/CODE&gt; or &lt;CODE&gt;chart&lt;/CODE&gt; for example.&lt;/P&gt;

&lt;P&gt;For the public holidays you would have to use a lookup.&lt;/P&gt;

&lt;P&gt;Cheers, MuS&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2014 05:28:47 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116645#M30982</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-09-09T05:28:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116646#M30983</link>
      <description>&lt;P&gt;HI MuS, I guess the above will give events which started between 8 and 18 hrs. what i am after is,when i calculate difference between close time and start time, i need to consider only from 9 AM to 5 PM&lt;/P&gt;

&lt;P&gt;e.g., start time=8/09/2014 9AM; end time= 9/09/2014 10AM&lt;BR /&gt;
when i do end time- start time, it should be 9 hrs&lt;/P&gt;</description>
      <pubDate>Tue, 09 Sep 2014 07:42:51 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116646#M30983</guid>
      <dc:creator>karthikTIL</dc:creator>
      <dc:date>2014-09-09T07:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116647#M30984</link>
      <description>&lt;P&gt;Not exactly. &lt;CODE&gt;date_hour&lt;/CODE&gt; starts at 0 (midnight to 1am) and ends with 23 (11pm to midnight). So using date_hour&amp;gt;8 starts at 9am and date_hour&amp;lt;18 ends at 5pm.&lt;/P&gt;

&lt;P&gt;use this run everywhere command:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;index=_internal date_hour&amp;gt;08 AND date_hour&amp;lt;18 NOT ( date_wday=Saturday OR date_wday=Sunday ) series | bucket _time span=1d | streamstats first(date_hour) AS first_hour, last(date_hour) AS last_hour by series,_time | eval diff=first_hour-last_hour | chart span=1d values(diff) AS diff over _time by series
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;this will calculate the diff for first hour to the last hour seen each day per series.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:31:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116647#M30984</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2020-09-28T17:31:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116648#M30985</link>
      <description>&lt;P&gt;HI MuS, Thank you. i got your point but don't know how exactly to include your statement in my query,Could you please le tme know.&lt;/P&gt;

&lt;P&gt;My current query is, &lt;BR /&gt;
source="file.csv" date_month=august|eval epoch_A=strptime('start_TIME',"%d/%m/%Y %I:%M:%S %p") | eval epoch_B=strptime('close_TIME',"%d/%m/%Y %I:%M:%S %p") | eval total_TIME=(('epoch_B'-'epoch_A') / 3600)|table start_TIME,close_TIME,total_TIME&lt;/P&gt;

&lt;P&gt;It converts close_time and start_time into epoch time and find the difference(24 hours day).Please let me know how to include your statement above to get total working hours.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:32:23 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116648#M30985</guid>
      <dc:creator>karthikTIL</dc:creator>
      <dc:date>2020-09-28T17:32:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116649#M30986</link>
      <description>&lt;P&gt;see my update&lt;/P&gt;</description>
      <pubDate>Wed, 10 Sep 2014 09:56:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116649#M30986</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-09-10T09:56:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116650#M30987</link>
      <description>&lt;P&gt;Sorry, it did not work.&lt;BR /&gt;
If start_time = 26/08/2014 3:23:29 PM and close_time=27/08/2014 12:15:18 PM , your query gives me total_time=20.86 hours, whereas i expect close_time = 4.52hours.&lt;BR /&gt;
Not sure if converting epoch time is culprit here.Or am i doing something wrong here?&lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 17:33:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116650#M30987</guid>
      <dc:creator>karthikTIL</dc:creator>
      <dc:date>2020-09-28T17:33:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116651#M30988</link>
      <description>&lt;P&gt;HI MuS, is there any update you want to provide please&lt;/P&gt;</description>
      <pubDate>Thu, 18 Sep 2014 06:57:06 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116651#M30988</guid>
      <dc:creator>karthikTIL</dc:creator>
      <dc:date>2014-09-18T06:57:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to find the difference between dates only counting business hours?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116652#M30989</link>
      <description>&lt;P&gt;okay, hard to test without any real data ... try this:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; source="file.csv" date_month=august date_hour&amp;gt;08 AND date_hour&amp;lt;18 NOT ( date_wday=Saturday OR date_wday=Sunday )
   | eval epoch_A=strptime('start_TIME',"%d/%m/%Y %I:%M:%S %p") 
   | eval epoch_B=strptime('close_TIME',"%d/%m/%Y %I:%M:%S %p") 
   | eval total_TIME=if((epoch_B-epochA)&amp;gt;57600, (epoch_B-epcoh_A-57600)/3600, (epoch_B-epcoh_A)/3600 ) 
   | table start_TIME, close_TIME, total_TIME
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The &lt;CODE&gt;if&lt;/CODE&gt; statement is based on the fact, that if the difference between start and end time is bigger as 57600 seconds (17 hours or 5pm to 9am) then this job was running over night and you simply minus those 17 hours from the total time.&lt;/P&gt;

&lt;P&gt;hope this helps ... and this is un-tested &lt;span class="lia-unicode-emoji" title=":winking_face:"&gt;😉&lt;/span&gt;&lt;/P&gt;

&lt;P&gt;cheers, MuS&lt;/P&gt;</description>
      <pubDate>Thu, 18 Sep 2014 08:11:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-to-find-the-difference-between-dates-only-counting-business/m-p/116652#M30989</guid>
      <dc:creator>MuS</dc:creator>
      <dc:date>2014-09-18T08:11:09Z</dc:date>
    </item>
  </channel>
</rss>

