【问题标题】:I have two input data picker fields, in first field it should be like 01/01/2022 and in the second field should have current date我有两个输入数据选择器字段,第一个字段应该是 01/01/2022,第二个字段应该是当前日期
【发布时间】:2022-12-20 21:45:20
【问题描述】:

  1. 我有 2 个输入日期字段 1.1.一个是开始日期,它应该像:= 01/01/current-year(01/01/2022,当前年份是 2022,所以它应该是今年,如果当前年份是 2023,那么它应该是这样的: = 01/01/2023)。

    1.2.第二个字段应该有当前日期:= 20/12/2022

    我已附上屏幕截图供您参考

    enter code here
    

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <meta content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Date Picker</title>
    </head>
    <body>
        <label for="start-date">Start Date</label>
        <input class="date-picker">
        <label for="end-date">End Date</label>
        <input class="date-picker">
    </body>
    </html>

    &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

    对此要求的任何帮助将不胜感激 提前致谢

【问题讨论】:

标签: javascript jquery bootstrap-5


【解决方案1】:

您好 Karthik,首先我注意到您应该为日期选择器元素提供一个 ID,以便单独定位它们。

html 将是这样的:

<label for="date-start">Start Date</label>
<input class="date-picker" id="date-start">
<label for="end-date">End Date</label>
<input class="date-picker" id="date-end">

然后javascript将是:

//current month and year
var yearNow = new Date().getFullYear();
var monthNow = new Date().getMonth() + 1;

//set the start date's field value to current year
document.getElementById('date-start').value = "01/01/" + yearNow;

//set the value of the end date field to current date
document.getElementById('date-end').value = monthNow + "/" + new Date().getDate() + "/" + yearNow;

我做了 .getMonth() + 1 因为 getMonth() 以 0 开头。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    • 2021-11-06
    • 2018-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多