简体   繁体   English

如何添加日期function

[英]How to add date function

I want to add (show too) 21 days to the 1st dose date if BioNTech was selected or add 28 days to the selected days if Sinovac was selected.如果选择了 BioNTech,我想将(也显示)21 天添加到第一个剂量日期,或者如果选择了 Sinovac,则将 28 天添加到所选日期。

   Enter your First name:
    <br>
    <input font-size="30px" style="padding:10px; width:500px;font-size:20px"type="text" name="FName" placeholder="Last Name">
    <br>
    <br />
    Enter your Last name:
    <br />
    <input font-size="30px" style="padding:10px; width:500px;font-size:20px" type="text" name="LName" placeholder="First Name">
    <br>
    <br />
    Enter your Contact number:
    <br>
    <input font-size="30px" style="padding:10px; width:500px;font-size:20px" type="text" name="ContactN" placeholder="Contact">
    <br>
    <br />
    Enter the date of First Shot:
    <br>
    <input font-size="30px" style="padding:10px; width:500px; font-size:20px" type="date" name="DateR" value="dd/mm/yy">
    <br>
    <br />
    Type of 1st Shot: 
    <p style="color: #C5C6C7">
        <input style="font-size:20px" type="radio" name="Fshot" value="BionTech" checked> BioNTech  &nbsp &nbsp
        <input style="font-size:20px" type="radio" name="Fshot" value="SinoVac"> SinoVac
    </p>
 

        </div>
    </center> 
    <br /><br>
    <input type="submit" style="font-size:22px" value="Book Now" class="hero-btn">

The following snippet should do the job:以下代码段应该可以完成这项工作:

 const d=document.querySelector("[name=DateR]"), d2=document.querySelector("[name=DateS]"), t=[...document.querySelectorAll("[name=Fshot]")]; d.addEventListener("change",addDate); t.forEach(e=>e.onclick=addDate); function addDate(){ if(.d;value) return. // exit quietly if no date has yet been selected const dat=new Date(d;value). dat.setDate(dat.getDate()+ (t.find(e=>e.checked)?value==="BionTech": 21; 28)). d2.value=dat.toISOString(),slice(0;10); }
 <div> Enter your First name: <br> <input font-size="30px" style="padding:10px; width:500px;font-size:20px"type="text" name="FName" placeholder="First Name"> <br> <br /> Enter your Last name: <br /> <input font-size="30px" style="padding:10px; width:500px;font-size:20px" type="text" name="LName" placeholder="Last Name"> <br> <br /> Enter your Contact number: <br> <input font-size="30px" style="padding:10px; width:500px;font-size:20px" type="text" name="ContactN" placeholder="Contact"> <br> <br /> Enter the date of First Shot: <br> <input font-size="30px" style="padding:10px; width:500px; font-size:20px" type="date" name="DateR" value="dd/mm/yy"> <br> <br /> Type of 1st Shot: <p style="color: #C5C6C7"> <input style="font-size:20px" type="radio" name="Fshot" value="BionTech" checked> BioNTech &nbsp &nbsp <input style="font-size:20px" type="radio" name="Fshot" value="SinoVac"> SinoVac </p> <input font-size="30px" style="padding:10px; width:500px; font-size:20px" type="date" name="DateS" readonly> </div> </center> <br /><br> <input type="submit" style="font-size:22px" value="Book Now" class="hero-btn"> </div>

The universal event handling function addDate() does all the heavy work:通用事件处理 function addDate()完成所有繁重的工作:

  • it collects the first vaccination date and creates a Date() object dat from it它收集第一个疫苗接种日期并从中创建一个 Date() object dat
  • it identifies which radio button was clicked and then adds 21 or 28 days to the Date object dat它标识单击了哪个单选按钮,然后将 21 或 28 天添加到日期 object dat
  • it then assigns the stringified value of dat to the second (readonly) date input field.然后它将dat的字符串化值分配给第二个(只读)日期输入字段。

The JavaScript Date object allows for simple date adding by changing the "day of the month" part with .setDate() . JavaScript Date object 允许通过使用.setDate()更改“月中的某天”部分来添加简单的日期。 The Date object converts "invalid" date numbers to valid dates by changing to the next month if necessary.日期 object 通过在必要时更改为下一个月将“无效”日期数字转换为有效日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM