<?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 How can I calculate the date difference by two timestamp? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-the-date-difference-by-two-timestamp/m-p/354880#M104995</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;

&lt;P&gt;I want to calculate the difference between dates within two different dates, my search is as below:&lt;/P&gt;

&lt;P&gt;code 1: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats min(_time) as first_time, max(_time) as last_time from datamodel=A by A.tag
| eval diff_date= (last_time- first_time)/ 86400
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The result of code 1 can get some value, but it is not 100% accurate, for example, if my first_time is 10/23/2017 11:00pm and last_time is 10/24/2017 09:00am, the result I want is 1 day difference, I only focus on the date, but if I use above code, the date difference could be 0, cause it is within 24 hours.&lt;/P&gt;

&lt;P&gt;code 2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats min(_time) as first_time, max(_time) as last_time from datamodel=A by A.tag
| eval first_date = strptime(first_time,"%Y-%m-%d") 
| eval last_date = strptime(last_time,"%Y-%m-%d") 
| eval diff = tostring((last_date-first_date), "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I did a lot of research and someone mentioned that tostring function can also help. But my problem here is that I can't know why I can't get anything if I use strptime function, but I CAN get the correct date by using strftime function, however, still can't get anything for the diff column.&lt;/P&gt;

&lt;P&gt;Can anyone Kindly help me on this? I really getting crazy trying to figure out this problem.&lt;/P&gt;

&lt;P&gt;Thanks very much.&lt;/P&gt;

&lt;P&gt;S&lt;/P&gt;</description>
    <pubDate>Tue, 29 Sep 2020 17:57:02 GMT</pubDate>
    <dc:creator>sakuraWu1</dc:creator>
    <dc:date>2020-09-29T17:57:02Z</dc:date>
    <item>
      <title>How can I calculate the date difference by two timestamp?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-the-date-difference-by-two-timestamp/m-p/354880#M104995</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;

&lt;P&gt;I want to calculate the difference between dates within two different dates, my search is as below:&lt;/P&gt;

&lt;P&gt;code 1: &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats min(_time) as first_time, max(_time) as last_time from datamodel=A by A.tag
| eval diff_date= (last_time- first_time)/ 86400
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The result of code 1 can get some value, but it is not 100% accurate, for example, if my first_time is 10/23/2017 11:00pm and last_time is 10/24/2017 09:00am, the result I want is 1 day difference, I only focus on the date, but if I use above code, the date difference could be 0, cause it is within 24 hours.&lt;/P&gt;

&lt;P&gt;code 2:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;| tstats min(_time) as first_time, max(_time) as last_time from datamodel=A by A.tag
| eval first_date = strptime(first_time,"%Y-%m-%d") 
| eval last_date = strptime(last_time,"%Y-%m-%d") 
| eval diff = tostring((last_date-first_date), "duration")
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I did a lot of research and someone mentioned that tostring function can also help. But my problem here is that I can't know why I can't get anything if I use strptime function, but I CAN get the correct date by using strftime function, however, still can't get anything for the diff column.&lt;/P&gt;

&lt;P&gt;Can anyone Kindly help me on this? I really getting crazy trying to figure out this problem.&lt;/P&gt;

&lt;P&gt;Thanks very much.&lt;/P&gt;

&lt;P&gt;S&lt;/P&gt;</description>
      <pubDate>Tue, 29 Sep 2020 17:57:02 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-the-date-difference-by-two-timestamp/m-p/354880#M104995</guid>
      <dc:creator>sakuraWu1</dc:creator>
      <dc:date>2020-09-29T17:57:02Z</dc:date>
    </item>
    <item>
      <title>Re: How can I calculate the date difference by two timestamp?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-the-date-difference-by-two-timestamp/m-p/354881#M104996</link>
      <description>&lt;P&gt;Code 1 &lt;CODE&gt;eval first_date = strftime(first_time,"%Y-%m-%d-%H") | eval last_date = strftime(last_time,"%Y-%m-%d-%H")|eval firstdate = strptime(first_date,"%Y-%m-%d")|eval lastdate = strptime(last_date,"%Y-%m-%d-%H")|eval diff_date=(lastdate-firstdate)/86400 | fields diff_date&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;You can shorten the query  of course, but remove  |fields to see all fields and the calculations&lt;/P&gt;</description>
      <pubDate>Sat, 03 Feb 2018 11:54:46 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/How-can-I-calculate-the-date-difference-by-two-timestamp/m-p/354881#M104996</guid>
      <dc:creator>Sukisen1981</dc:creator>
      <dc:date>2018-02-03T11:54:46Z</dc:date>
    </item>
  </channel>
</rss>

