0

我有一个字段名称,称为"REF_DATE". "REF_DATE"'s格式"YYYY/MM/DD"为.

我需要计算从今天到"REF_DATE".

例如:

REF_DATE = 2020/01/09

Today = 2020/04/01

以下是我试过的代码:

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>";

xpages表现出来"853"

所以,我需要你们帮助我计算日期之间的天数。

谢谢!!!!!!

4

1 回答 1

1

将年份作为第一个 Date.UTC 参数传递并从月份中减去 1(0 是一月):

<?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>
于 2020-04-02T13:54:07.217 回答