4

I have the following line of code that pulls a list field date and stores it in a variable called dueDate:

var dueDate = new Date(ctx.CurrentItem.ReminderDate);

How do I create another variable or set the existing one plus 15 days?

Robert Lindgren
  • 24,520
  • 12
  • 53
  • 79
Luis Carvalho
  • 483
  • 1
  • 11
  • 27

1 Answers1

4

Dates are calculated in milliseconds (since 1/1/1970)

with

  • 1000 milliseconds in a second
  • 60 seconds in a minute
  • 60 minutes in an hour
  • 24 hours in a day

Each day is 1000*60*60*24 = 86400000 milliseconds

to prevent typos and type less characters you can use the scientific notation: 864e5

So to add 15 days, the JavaScript code is:

var dueDate = new Date(ctx.CurrentItem.ReminderDate);
var anotherDate = new Date( dueDate/1 + 864e5*15 );

The important part is dueDate/1 , where a Date value is converted to milliseconds.

Note that this includes the time part of the date notation, 15*24 hours are added.

If you are developing conditional color formatting with CSR,
see: Conditional Formatting Based on Number Range

CSR note: the this scope in a CSR called function references ctx.CurrentItem

so

var dueDate = new Date(ctx.CurrentItem.ReminderDate);

can be written as:

var dueDate = new Date(this.ReminderDate);
Danny '365CSI' Engelman
  • 21,176
  • 7
  • 35
  • 79