I need to reduce a date by an integer of days. In this example I’m reducing the date currently in the field “calldate” to 7 days earlier.
The following works just fine if I’m resetting a date field on a table. However, exactly the same methodology fails if I replace the date field with a date variable (stubbornly stays in milliseconds). And that’s what I need – a date variable which I can then use in a SQL query.
So here’s the method, first, using a table date field. This works. It converts the current contents of the table’s date field into milliseconds since 1Jan1970, multiplies the milliseconds in a day times 7, subtracts one millisecond number from the other. Finally resets the target date field using the calculated milliseconds.
// get the debugger to show in date format
var target = calldate;
// next step displays as a date without this step
var milli = null;
// get the target date in milliseconds
milli = target.getTime();
// the number of days to be subtracted from the field "calldate"
var pref = 7;
// the number of milliseconds in a day = 86400000
milli = milli - (pref * 86400000);
// reset calldate; displays as milliseconds
calldate = calldate.setTime(milli);
// get the debugger to show in date format
var end = calldate;
Now here’s the same thing again. All I’ve done is replace the date field “calldate” with a variable date “today”. It doesn’t work.
var target = new Date();
var milli = null;
milli = target.getTime();
var pref = 7;
milli = milli - (pref * 86400000);
today = today.setTime(milli);
var end = new Date();
var end = today;