All Apps and Add-ons

In the following query, what is wrong with this date difference?

collinlorb
Engager

Below is my code: I have followed suggestions found on here for date & time difference, yet this one is not working. What's wrong with it?

| from datamodel:"TestTable"
| where scope LIKE "Test%"
| where us_schedule_state like "Accepted"
| where !LIKE(iteration, "%G") 
| eval EndDate = strptime(AcceptedDate, "%Y-%m-%d") 
| eval StartDate = strptime(trans_to_inprogress_date, "%Y-%m-%d") 
| eval DateDiff = tostring((EndDate - StartDate), "duration")
| eval Date = strftime(1533096000.000000, "%Y-%m-%d %H:%S")
| where strftime(StartDate, "%Y-%m-%d %H:%S") > Date
Tags (1)
0 Karma

somesoni2
Revered Legend

Both operands in your last where clause are string. To compare dates, use the epoch formatted value.

| from datamodel:"TestTable"
 | where scope LIKE "Test%"
 | where us_schedule_state like "Accepted"
 | where !LIKE(iteration, "%G") 
 | eval EndDate = strptime(AcceptedDate, "%Y-%m-%d") 
 | eval StartDate = strptime(trans_to_inprogress_date, "%Y-%m-%d") 
 | eval DateDiff = tostring((EndDate - StartDate), "duration")
 | eval Date = strftime(1533096000.000000, "%Y-%m-%d %H:%S")
 | where StartDate > strptime(Date, "%Y-%m-%d %H:%S")

OR simply

| from datamodel:"TestTable"
 | where scope LIKE "Test%"
 | where us_schedule_state like "Accepted"
 | where !LIKE(iteration, "%G") 
 | eval EndDate = strptime(AcceptedDate, "%Y-%m-%d") 
 | eval StartDate = strptime(trans_to_inprogress_date, "%Y-%m-%d") 
 | eval DateDiff = tostring((EndDate - StartDate), "duration")
 | eval Date = strftime(1533096000.000000, "%Y-%m-%d %H:%S")
 | where StartDate > 1533096000.000000
0 Karma

morethanyell
Builder
| where strftime(StartDate, "%Y-%m-%d %H:%S") > Date

the code above parses Hour : Second which doesn't make sense

also, try this

| eval EndDate = strptime(AcceptedDate . " 00:00:00", "%Y-%m-%d %H:%M:%S") 
| eval StartDate = strptime(trans_to_inprogress_date . " 00:00:00", "%Y-%m-%d %H:%M:%S") 

EDIT

if above doesn't work because you mentioned the time format looks like "2018-11-16T14:10:20.969Z", try this:

| rex field=AcceptedDate "(?<date_ymd_ad>[^/]*)T(?<date_hms_ad>[^//]*)."
| rex field=trans_to_inprogress_date "(?<date_ymd_ttid>[^/]*)T(?<date_hms_ttid>[^//]*)."
| eval EndDate = strptime(date_ymd_ad . " " . date_hms_ad, "%F %X.%3N")
| eval StartDate = strptime(date_ymd_ttid . " " . date_hms_ttid, "%F %X.%3N")
| eval DateDiff = tostring(round(EndDate - StartDate, 3), "duration")
0 Karma

collinlorb
Engager

That code actually returns nothing for StartDate or EndDate

0 Karma

collinlorb
Engager

Could this be because of the format of the field that I am using strptime for in the first place? Here is what it looks like.

2018-11-16T14:56:20.969Z

0 Karma

morethanyell
Builder
| rex field=AcceptedDate "(?<date_ymd_ad>[^/]*)T(?<date_hms_ad>[^//]*)."
| rex field=trans_to_inprogress_date "(?<date_ymd_ttid>[^/]*)T(?<date_hms_ttid>[^//]*)."
| eval EndDate = strptime(date_ymd_ad . " " . date_hms_ad, "%F %X.%3N")
| eval StartDate = strptime(date_ymd_ttid . " " . date_hms_ttid, "%F %X.%3N")
| eval DateDiff = tostring(round(EndDate - StartDate, 3), "duration")
0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.

Can’t make it to .conf25? Join us online!

Get Updates on the Splunk Community!

Can’t Make It to Boston? Stream .conf25 and Learn with Haya Husain

Boston may be buzzing this September with Splunk University and .conf25, but you don’t have to pack a bag to ...

Splunk Lantern’s Guide to The Most Popular .conf25 Sessions

Splunk Lantern is a Splunk customer success center that provides advice from Splunk experts on valuable data ...

Unlock What’s Next: The Splunk Cloud Platform at .conf25

In just a few days, Boston will be buzzing as the Splunk team and thousands of community members come together ...