This topic is locked
[SOLVED]

 Subtract Date not working correctly

8/31/2016 8:54:32 PM
PHPRunner General questions
M
Mark Kramer author

Hi all,
I have a table that stores the date in "DateOpened" field when work order is open and when it is closed via a check box, it auto populates the "DateClosed" (with now()) and the " TimeOpen" fields which works as it should. The problem I am running into is date will subtract hours,min,seconds but will not subtract the day. In other words if I opened a ticket yesterday at 12:00pm and closed it today at 2:00pm it shows only open for 2 hours not 1 day and 2 hours or 22 hours. Here is my jscript code used in the Onload event..
Thanks for your help in advance..
**

var d = new Date();
var ctrlCheck= Runner.getControl(pageid, 'Closed');
var ctrlDate = Runner.getControl(pageid, 'DateClosed');
var TO = Runner.getControl(pageid, 'TimeOpen');
ctrlCheck.on('change', function(e){

if (this.getValue() == 'on'){

ctrlDate.setValue(d);

}
var CD = Runner.getControl(pageid, 'DateOpened');
now = new Date();
var DO=new Date(CD.getValue());
var age=Math.floor(( Date.parse(now) - Date.parse(DO)+ (24 60 60 365 1000))); ;
milliseconds = age

mydate=new Date(milliseconds);
humandate=mydate.getUTCDay()+" day(s), "+mydate.getUTCHours()+" hours, "+mydate.getUTCMinutes()+" minutes and "+mydate.getUTCSeconds()+" second(s)";
TO.setValue(humandate);
});

M
Mark Kramer author 9/1/2016

Maybe it's the inspiration of everyone here or just plain hacking until my eyes hurt but I solved my own issue once again by starting from scratch and rewriting the code with a combination of examples found all over the internet. So here it is in the hopes it might help someone else:[/i] <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=80196&image=1&table=forumreplies' class='bbc_emoticon' alt='B)' />
//*****

var d = new Date(); //date at this very moment

var ctrlCheck= Runner.getControl(pageid, 'Closed');

var ctrlDate = Runner.getControl(pageid, 'DateClosed');

var TO = Runner.getControl(pageid, 'TimeOpen'); // Time open field to set value to when checked "close"
//****

ctrlCheck.on('change', function(e){

if (this.getValue() == 'on'){

ctrlDate.setValue(d); //date at this very moment entered in the "DateClosed" field when "closed " is clicked

}
var CD = Runner.getControl(pageid, 'DateOpened'); //Control (field Date) entered during add record
//*

var DO = new Date(CD.getValue()); //date opened

var DOP = Date.parse(DO) // Grab value and covert it to 'ms' so it is an usable number instead of a string

var dp = Date.parse(d) // Grab value and covert it to 'ms' so it is an usable number instead of a string
// get total seconds between the times

var delta = Math.abs(dp - DOP) / 1000;
// calculate (and subtract) whole days

var days = Math.floor(delta / 86400);

delta -= days 86400;
// calculate (and subtract) whole hours

var hours = Math.floor(delta / 3600) % 24;

delta -= hours
3600;
// calculate (and subtract) whole minutes

var minutes = Math.floor(delta / 60) % 60;

delta -= minutes * 60;
// what's left is seconds

var seconds = delta % 60;
tto =(days)+" day(s) "+(hours)+" hr(s) "+(minutes)+" min "+(seconds)+" sec "
TO.setValue(tto);
});