cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Data.parse is not working using javascript

nareshkumarg
Observer
Date.parse("8/22/24 1:37:08 PM") is giving me a negative value under Dynatrace but when I validated it under Visual Studio code it worked fine.
2 REPLIES 2

jaume_reverte
Dynatrace Advisor
Dynatrace Advisor

Hello Nares, 

I used the Date.parse that you sent and is working for me inside a JavaScript code execution:

jaume_reverte_0-1724398372028.png

Can you send the code that you are using inside Dynatrace so I can help you more? 

Hope you a good monitoring!
Jaume Reverte

Hi Jaume,

Sample code and payload given below,

responseBody = "{\"data\":[{\"jobNumber\":1000001207847,\"status\":\"S\",\"project\":\"ProdDT\",\"userName\":\"mftstageadmineu\",\"endTime\":\"8/23/24 10:37:38 AM\"},{\"jobNumber\":1000001207846,\"status\":\"S\",\"project\":\"ProdDT\",\"userName\":\"mftstageadmineu\",\"endTime\":\"8/23/24 10:37:35 AM\"},{\"jobNumber\":1000001207845,\"status\":\"S\",\"project\":\"AzureStage\",\"userName\":\"mftprojectlaunch\",\"endTime\":\"8/23/24 10:37:32 AM\"},{\"jobNumber\":1000001207844,\"status\":\"S\",\"project\":\"PingProd\",\"userName\":\"mftstageadmineu\",\"endTime\":\"8/23/24 10:37:35 AM\"},{\"jobNumber\":1000001207843,\"status\":\"S\",\"project\":\"PingProd\",\"userName\":\"mftstageadmineu\",\"endTime\":\"8/23/24 10:37:38 AM\"}]}"


if (response.getStatusCode() != 200) {
api.fail("HTTP error: " + response.getStatusCode());
}
var responseBody = response.getResponseBody();
var jsonData = JSON.parse(responseBody);

for (j = 0; j < (jsonData.data).length; j++) {
var ProdDTJobTime = (jsonData.data)[j].endTime + ' GMT';
var ProdDTJobResult = Date.now() - Date.parse(ProdDTJobTime) < 86400000 ? "triggered now" : "triggered later";
if ((jsonData.data)[j].project === "ProdDT") {
if (ProdDTJobResult === "triggered later") {
api.fail("New job was not triggered for " + (jsonData.data)[j].project + " Last triggered time in UTC was : " + (jsonData.data)[j].endTime);
break;
}
api.setValue("ProdDT", (jsonData.data)[j].jobNumber);
api.info((jsonData.data)[j].jobNumber);
break;
}
}

Featured Posts