0

I'm trying to compare the date of today with the date in a date field from a SP listitem.

The date of today is returned like: [date] Thu Mar 29 12:09:08 UTC+0200 2018 and the date in my field (LTIOV) like: [date] Sun Jul 5 00:00:00 UTC+0200 2020

The second date is wrong as in the listitem it is : 31-5-2018

I tried the following javascript to compare but it doesn't work:

var date = new Date(listItem.LTIOV);
var todaysDate = new Date();

console.log(todaysDate);
console.log(date);

if ((date < todaysDate) && (listItem.MijnStatus == "In Action")) {
if (row != null)
    row.style.backgroundColor = "rgba(153, 204, 255, 0.5)"; //light blue
}

How can I compare these two?

Marco
  • 85
  • 1
  • 14
  • After a lot of puzzling I found the solution: var parts = listItem.LTIOV.split('-'); var todaysDate = new Date(); var date = new Date(parts[2], parts[1] - 1, parts[0]); if ((date < todaysDate) && (listItem.MijnStatus == "In Action")) { if (row != null) row.style.backgroundColor = "rgba(252, 213, 192, 0.5)"; //light orange } – Marco Mar 29 '18 at 12:01
  • So a duplicate of [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Mar 30 '18 at 04:49

2 Answers2

0

After a lot of puzzling I found the solution:

var parts = listItem.LTIOV.split('-');
var todaysDate = new Date();
var date = new Date(parts[2], parts[1] - 1, parts[0]);

if ((date < todaysDate) && (listItem.MijnStatus == "In Action")) {
if (row != null)
    row.style.backgroundColor = "rgba(252, 213, 192, 0.5)"; //light orange
}
Marco
  • 85
  • 1
  • 14
0

This could also be possible:

var itemDate = new Date(listItem.LTIOV).getTime();
var todaysDate = new Date().getTime();

var dateIsBeforeToday = itemDate < todaysDate;

With this solution, you wouldn't have to split which is not really safe.

sandrooco
  • 8,016
  • 9
  • 48
  • 86