This topic is locked

Subtracting date and time with Javascript

7/28/2019 3:09:29 AM
PHPRunner Tips and Tricks
M
Mark Kramer author

Time and Dates can be a real pain when it adding and subtracting. Below is the code using the "JavaScript Math Object" that makes it easier to deal with.
"Javascript onload event of the page calculations are needed"
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;
=(days)+" day(s) "+(hours)+" hr(s) "+(minutes)+" min "+(seconds)+" sec "
[color="#FF0000"]TO.setValue(tto);
Red of course are your own varables..