过去与日期比较条件

huangapple go评论88阅读模式
英文:

Past and date comparison condition

问题

以下是代码的翻译部分:

<tr>
    <td><label for="maintsd">维护开始日期 (<%=tzShort%>)</label></td>
    <td><input style="border:1px solid grey; width: 100%" type="datetime-local" id="maintsd" required="true" name="maintsd"></td><td></td>
</tr>
<tr>
    <td>
        <label for="mainted">维护结束日期 (<%=tzShort%>)</label>
    </td>
    <td>
        <input style="border:1px solid grey; width: 100%" type="datetime-local" id="mainted" required="true" name="mainted">
    </td>                                                       
</tr>

以下是日期被保存的代码:

public String getMaintEDString() {
    //  ZoneId z = ZoneId.systemDefault();
    LocalDateTime datetime = LocalDateTime.ofInstant(this.maintED, ZoneId.systemDefault());
    this.maintEDString = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm").format(datetime);
    return maintEDString;
}

public String getMaintSDString() {
    //   ZoneId z = ZoneId.of("America/Los_Angeles");
    LocalDateTime datetime = LocalDateTime.ofInstant(this.maintSD, ZoneId.systemDefault());
    this.maintSDString = DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm").format(datetime);
    return maintSDString;
}
英文:

With the below code, I'm unable to set the following:

  1. Start date should be of future
  2. End date should be higher than start date

<!-- lang: jsp -->

&lt;tr&gt;
    &lt;td&gt;&lt;label for=&quot;maintsd&quot;&gt;Maintenance Start Date (&lt;%=tzShort%&gt;)&lt;/label&gt;&lt;/td&gt;
    &lt;td&gt;&lt;input style=&quot;border:1px solid grey; width: 100%&quot; type=&quot;datetime-local&quot; id=&quot;maintsd&quot; required=&quot;true&quot; name=&quot;maintsd&quot;&gt;&lt;/td&gt;&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
    &lt;td&gt;
        &lt;label for=&quot;mainted&quot;&gt;Maintenance End Date (&lt;%=tzShort%&gt;)&lt;/label&gt;
    &lt;/td&gt;
    &lt;td&gt;
        &lt;input  style=&quot;border:1px solid grey; width: 100%&quot;  type=&quot;datetime-local&quot; id=&quot;mainted&quot; required=&quot;true&quot; name=&quot;mainted&quot;&gt;
    &lt;/td&gt;                                                       

Date is in T format.

Date which is being saved is retrieved as below:

 public String getMaintEDString() {
  //  ZoneId z = ZoneId.systemDefault();
    LocalDateTime datetime = LocalDateTime.ofInstant(this.maintED, ZoneId.systemDefault());
    this.maintEDString = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd hh:mm&quot;).format(datetime);
    return maintEDString;
}

public String getMaintSDString() {
  //   ZoneId z = ZoneId.of( &quot;America/Los_Angeles&quot; );
    LocalDateTime datetime = LocalDateTime.ofInstant(this.maintSD, ZoneId.systemDefault());
    this.maintSDString = DateTimeFormatter.ofPattern(&quot;yyyy-MM-dd hh:mm&quot;).format(datetime);
    return maintSDString;
}

答案1

得分: 1

你可以在输入框中使用min属性,并设置最小日期,这将禁用所有先前的日期,例如:min="&lt;%=tzShort%&gt;",然后在点击提交按钮时获取开始和结束日期两个输入,比较这两个值,根据比较结果显示错误消息或提交表单。

演示代码 :

<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function validate() {
  // 获取开始和结束日期并转换为日期对象
  var sdate = new Date(document.getElementById("maintsd").value);
  var edate = new Date(document.getElementById("mainted").value);
  var flag = true; // 用于验证的标志
  // 如果结束日期大于等于开始日期
  if (edate >= sdate) {
    console.log("结束日期大于开始日期");
    document.getElementById("error").innerHTML = "";
  } else {
    flag = false;
    document.getElementById("error").innerHTML = "*结束日期应大于开始日期"; // 显示此消息
  }
  return flag;
}
<!-- language: lang-html -->
<!--onsubmit 会调用 validate 函数-->
<form onsubmit="return validate();">
  <table>
    <tr>
      <td><label for="maintsd">维护开始日期 (&lt;%=tzShort%&gt;)</label></td>
      <td><input style="border:1px solid grey; width: 100%" type="datetime-local" id="maintsd" required="true" name="maintsd" value="2020-08-12T10:22" min="2020-08-12T10:22"></td>
      <!-- 添加最小日期以禁用过去的日期-->

    </tr>
    <tr>
      <td>
        <label for="mainted">维护结束日期 (&lt;%=tzShort%&gt;)</label>
      </td>
      <td>
        <input style="border:1px solid grey; width: 100%" type="datetime-local" id="mainted" required="true" name="mainted" value="2020-08-19T10:22" min="2020-08-12T10:22">

      </td>
    </tr>
  </table>
  <p id="error" style="color:red"></p>
  <input type="submit">
</form>
<!-- end snippet -->
英文:

You can use min attribute in your input-box and set there min-date this will disable all previous date i.e : min=&quot;&lt;%=tzShort%&gt;&quot; and then on click of submit button get both inputs i.e : start and end date compare both values and depending on this show error message or submit form.

Demo Code :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

function validate() {
  //get start and end date and convert to dates 
  var sdate = new Date(document.getElementById(&quot;maintsd&quot;).value);
  var edate = new Date(document.getElementById(&quot;mainted&quot;).value);
  var flag = true; //for validating
  //if end date is greater
  if (edate &gt;= sdate) {
    console.log(&quot;end_date is greater then strt_date&quot;);
    document.getElementById(&quot;error&quot;).innerHTML = &quot;&quot;;
  } else {
    flag = false;
    document.getElementById(&quot;error&quot;).innerHTML = &quot;*end_date should be  greater then strt_date&quot;; //show this message

  }
  return flag;
}

<!-- language: lang-html -->

&lt;!--onsubmit validate will get called--&gt;
&lt;form onsubmit=&quot;return validate();&quot;&gt;
  &lt;table&gt;
    &lt;tr&gt;
      &lt;td&gt;&lt;label for=&quot;maintsd&quot;&gt;Maintenance Start Date (&lt;%=tzShort%&gt;)&lt;/label&gt;&lt;/td&gt;
      &lt;td&gt;&lt;input style=&quot;border:1px solid grey; width: 100%&quot; type=&quot;datetime-local&quot; id=&quot;maintsd&quot; required=&quot;true&quot; name=&quot;maintsd&quot; value=&quot;2020-08-12T10:22&quot; min=&quot;2020-08-12T10:22&quot;&gt;&lt;/td&gt;
      &lt;!-- add min date so backdates with be disable--&gt;

    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;label for=&quot;mainted&quot;&gt;Maintenance End Date (&lt;%=tzShort%&gt;)&lt;/label&gt;
      &lt;/td&gt;
      &lt;td&gt;
        &lt;input style=&quot;border:1px solid grey; width: 100%&quot; type=&quot;datetime-local&quot; id=&quot;mainted&quot; required=&quot;true&quot; name=&quot;mainted&quot; value=&quot;2020-08-19T10:22&quot; min=&quot;2020-08-12T10:22&quot;&gt;

      &lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;
  &lt;p id=&quot;error&quot; style=&quot;color:red&quot;&gt;&lt;/p&gt;
  &lt;input type=&quot;submit&quot;&gt;
&lt;/form&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年9月29日 21:00:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/64120186.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定