我有2个日期,即现在的日期和开始日期。如果开始日期是过去,则输出将显示0作为值。但是,如果开始日期和当前日期相同,则将根据用户输入显示值。
这是我到目前为止所尝试的。日期显示但第一个索引仍然打印0,而日期相同时不是用户输入。我很确定我在这里做错了什么。任何帮助将不胜感激。谢谢。
<label1>Present Value as of date</label1> <input id="presentDate" type="date">
<label2>Start date</label2> <input id="startDate" type="date">
var paymentDate = document.getElementById("presentDate").value;
var startDate = document.getElementById("startDate").value;
if (paymentDate)
plDate = new Date(paymentDate);
var pdates = format(new Date(plDate), 'yyyy-MM-dd').split('-');
paymentDayValue = pdates[2];
var presentDate, endDate,stsDate,sDay;
if (paymentDate)
presentDate = new Date(paymentDate);
if (startDate)
stsDate = new Date(startDate);
var firstIndex=0;
for (var i = 0; i < dates.length; i++) {
if (dates[i]) {
var splitDate = dates[i].split('-');
var diffdate = splitDate[1] + "/" + presentDate.getUTCDate() + "/" + splitDate[0];
var sdiffdate = splitDate[1] + "/" + stsDate.getUTCDate() + "/" + splitDate[0];
if (i == firstIndex)
htmlString += "<tr><td>" + diffdate + "</td><td>" + "$" + 0 + " </td></tr>";
else if (i == firstIndex && diffdate == sdiffdate)
htmlString += "<tr><td>" + sdiffdate + "</td><td>" + "$" + rent + " </td></tr>";
答案 0 :(得分:1)
你的代码有点混乱,我不确定你到底想要做什么,但我认为这是你需要的逻辑:
// paymentDate is a text variable
var paymentDate = document.getElementById("presentDate").value;
// startDate is a text variable
var startDate = document.getElementById("startDate").value;
// create new Date objects
var datePayment = new Date(paymentDate)
var dateStart = new Date(startDate)
// compare the dates
var sameDates = (datePayment.getTime() === dateStart.getTime())
var notSameDates = (datePayment.getTime() !== dateStart.getTime())