-1

I am trying to use the =>, =<, == comparison operators on the date object to determine if a document was created in the past 3 days then display new next to the title if true.

Right now I have:

var createdDate = new Date((ctx.CurrentItem.Created)); 
var threeDaysAgo = new Date((new Date()).valueOf() - 1000*60*60*24*3); 

                        
                        
if (createdDate.valueOf() >= threeDaysAgo.valueOf())
                        {
                          then display *new*
                        }

In this case I turned the date into milliseconds to compare, but it doesnt work. Ive tried formatting the date in multiple different ways and then comparing... still doesnt work. I've also tried using .getTime(), and it doesnt work. When I change >= to <= then it displays new for all documents, even those created 10 years ago, so atleast I know the code is correct inside the if statement.

Thanks for the help!

  • 1
    Comparing valueOf() should work. Check the individual values (add a breakpoint or use console.log). – kol Jul 26 '21 at 15:51
  • Define "doesn't work". Subtracting days by milliseconds is flawed, see [*Add days to JavaScript Date*](https://stackoverflow.com/questions/563406/add-days-to-javascript-date). – RobG Jul 26 '21 at 20:21
  • What is the format of *ctx.CurrentItem.Created*? See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results) – RobG Jul 27 '21 at 00:34

2 Answers2

2

Try like this:

var createdDate = new Date('2021-07-25T03:24:00');
var treeDaysAgo = new Date();
treeDaysAgo.setDate(treeDaysAgo.getDate() - 3);

After this this will evaluate to true:

createdDate >= treeDaysAgo

And if you change createDate to earlier, it will evaluate to false.

Maybe you have ctx.CurrentItem.Created in a format that is not suitable for Date construction.

Peter Koltai
  • 8,296
  • 2
  • 10
  • 20
  • 1
    Thanks for the help, I tried your suggestions, and it still doesn't work. After looking into it, I think your right, the problem is most likely the fact that ctx.currentitem.created is a pretty weird format. I will find a solution and let you know if you are curious. – Magestic_savage Jul 26 '21 at 16:12
  • Feel free to paste a ctx.CurrentItem.Created example here maybe I can provide hints. – Peter Koltai Jul 26 '21 at 16:37
0

The same issue was disscussed here: https://sharepoint.stackexchange.com/questions/166784/how-can-i-convert-the-format-ctx-currentitem-created

enter image description here

You could change the string with:

var datestring="2015/10/20 PM 06:09";
var dateparts=datestring.split(' ');//array
if(dateparts[1].length===2){//am or pm in second array element?
  dateparts.push(dateparts[1]);//move it to the end
  dateparts.splice(1,1);//take out the am/pm in the middle
}
var mydate=dateparts.join(' ');//make it one string
console.log(mydate);
console.log(new Date(mydate).toString());
console.log(new Date(mydate).toISOString());
Michael Han
  • 3,475
  • 1
  • 6
  • 8