Reducing a date by an integer of days

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;

Hi Morley,

It can be done in 1 line:

var end = new Date(calldate - (7 * 1000 * 60 * 60 * 24));

Or when the date as to be (re)stored in calldate this would be:

calldate = new Date(calldate - (7 * 1000 * 60 * 60 * 24));

This calculation doesn’t touch the time part of the date, it just substracts 7 whole days.

Another method is:

d1 = new Date();

d1.setDate(d1.getDate() - 7);

application.output(d1.toLocaleString());

I like this one:

  • 1 day:
return globals.date1.setDate(globals.date1.getDate()+1); 
  • 2 days
return globals.date1.setDate(globals.date1.getDate()+2); 

-3 days

return globals.date1.setDate(globals.date1.getDate()-3); 

have fun!
:)

IT2BE:
Another method is:

d1 = new Date();

d1.setDate(d1.getDate() - 7);

application.output(d1.toLocaleString());

you where just quicker than me! :lol:

hello
this is all verry interesting thanks

but what if i want to go 1 month back in time

and can i get the format in dd-MM-yyyy
with this i get format “Wed Dec 14 11:18:40 CET 2005”

return globals.date1.setDate(globals.date1.getDate()-3);

var d = new Date();
var month = d.getMonth();

d.setMonth(month - 1);
application.output(d);

hope this helps

Use a combination of get(Date/Month) and set(Date/Month

myDate.setMonth(myDate.getMonth() -1); // to go back one month

also

myDate.setDate(myDate.getDate() + 7); // to go forward by 7 days

Graham Greensall
Worxinfo Ltd