0

I have a field name, called "REF_DATE". The "REF_DATE"'s format is "YYYY/MM/DD".

I need to calculate the number of days from today to the "REF_DATE".

For example:

REF_DATE = 2020/01/09

Today = 2020/04/01

The following is I tried the code:

var s1 = "2020/01/09";
var s2 = "2020/04/01";
var aDate = s1.split("/");
var oDate1 = Date.UTC(aDate[1],aDate[2],aDate[0]);
var bDate = s2.split("/");
var oDate2 = Date.UTC(bDate[1],bDate[2],bDate[0]);
var iDays = Math.floor((oDate2 - oDate1)/86400000);
return oDate1+"<br>"+oDate2+"<br>"+iDays+"<br>";

But the xpages showed "853".

So, I need you guys to help me with how to calculate the number of days between dates.

Thanks!!!!!!

kedar sedai
  • 1,687
  • 3
  • 16
  • 25
Ariel
  • 57
  • 6
  • 2
    When I run the code above I get `83` – Luís Ramalho Apr 01 '20 at 07:26
  • 1
    That code might yield different results because of daylight savings. – str Apr 01 '20 at 07:33
  • I tried that method "new Date(aDate[1], aDate[2], aDate[0]);".But the answer is "853"..... – Ariel Apr 01 '20 at 07:37
  • 1
    See this answer for a working solution: https://stackoverflow.com/a/15289883/906113 – str Apr 01 '20 at 07:39
  • If I used the "console.log()", the code can't work..... – Ariel Apr 01 '20 at 07:45
  • 2
    "the code can't work" is not helpful. What happens? Do you get an error? It works for me. Depending on your browser, you might run into a problem because you are using [invalid date time formats](https://stackoverflow.com/questions/51715259/what-are-valid-date-time-strings-in-javascript). But again: See the answer in the duplicate question. – str Apr 01 '20 at 07:48
  • I wrote this code in XPages of IBM Domino Designer. And in the web browser preview(Google). After I ran the code, the web browser is blank. So I don't know where is the problem of the code. – Ariel Apr 01 '20 at 07:56
  • 1
    @str: "It works for me." is not helpful. You did not even ask which kind of JS engine Ariel is using... – Sven Hasselbach Apr 01 '20 at 20:15
  • Arguments are passed in the wrong order. Month should be offset by one to be between 0 and 11. – shawnt00 Apr 08 '20 at 03:59
  • It says `(year, month, day)` here: https://help.hcltechsw.com/dom_designer/9.0.1/reference/r_wpdr_standard_date_utc_r.html – shawnt00 Apr 08 '20 at 04:13

1 Answers1

1

Pass the year as the first Date.UTC parameter and subtract 1 from the month (0 is January):

<?xml version="1.0" encoding="UTF-8"?>
<xp:view xmlns:xp="http://www.ibm.com/xsp/core">    
<xp:this.beforePageLoad><![CDATA[#{javascript:
    var s1 = "2020/01/09";
    var s2 = "2020/04/01";
    var aDate = s1.split("/");
    var oDate1 = Date.UTC(Number(aDate[0]), Number(aDate[1]-1), Number(aDate[2]));

    var aDate = s2.split("/");
    var oDate2 = Date.UTC(Number(aDate[0]), Number(aDate[1]-1), Number(aDate[2]));

    var iDays = Math.floor((oDate2 - oDate1) /1000/60/60/24);
    print(iDays);
}]]></xp:this.beforePageLoad>
</xp:view>
teleman
  • 940
  • 7
  • 24
  • Thanks for your answer! But the "console.log();" isn't work for my JS engine. And the result still showed "853"...... – Ariel Apr 07 '20 at 02:10
  • This works for you but not for me. What's your JS engine? My JS engine is "XPages". – Ariel Apr 08 '20 at 01:00
  • 1
    Yes, it works in XPages. I have modified the answer again with the full XPage. Have you corrected your mistake in order of year, month, day? – teleman Apr 09 '20 at 15:54
  • I used your code on Apr 7. So the code didn't work for me. After you edited on Apr 9, I used your code on Apr 10. Now, the code can work for me in XPages. I really appreciate your answer. Thanks!!!!!!!! – Ariel Apr 14 '20 at 09:24