猿问

将日期格式从 MM/DD/YYYY 编辑为 DD/MM/YYY

所以我编写了一些代码,允许某人输入他们的订单日期并返回预期的打印和交货日期。


唯一的问题是它拆分了 MM/DD/YYYY。非常感谢任何帮助使这项工作正常进行。


<head>

  <meta charset="utf-8">

  <meta name="viewport" content="width=device-width, initial-scale=1">

  <title>jQuery UI Datepicker - Default functionality</title>

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

  <link rel="stylesheet" href="/resources/demos/style.css">

  <script src="https://code.jquery.com/jquery-1.12.4.js"></script>

  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

  <script>

    $(function() {

      $("#datepicker").datepicker();

    });

  </script>


  <script>

    function myfunction() {


      var future = new Date(document.getElementById("datepicker").value); // get today date

      future.setDate(future.getDate() + 7); // add 7 days




      var finalDate = future.getFullYear() + '-' + ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) + '-' + future.getDate();



      var future2 = new Date(document.getElementById("datepicker").value);

      future2.setDate(future2.getDate() + 10); // add 7 days



      var finalDate2 = future.getFullYear() + '-' + ((future2.getMonth() + 1) < 10 ? '0' : '') + (future2.getMonth() + 1) + '-' + future2.getDate();





      alert('Your order will be printed on ' + finalDate + '\nYou should recieve your order ' + finalDate2);

    }

  </script>

</head>


<body>

  <form onSubmit="myfunction()">

    <p>Date: <input type="text" id="datepicker" name="date"></p>

    <input type="submit" lable="Submit">


    <p id="demo"></p>


  </form>




</body>


</html>


慕神8447489
浏览 78回答 1
1回答

慕容708150

我在您的代码上以日期格式进行编辑:var finalDate和var finalDate2。因为在代码中在 jquery 中设置了日期格式。在下面的代码中,将日期格式更改为 DD/MM/YYYY所以我更改为以下代码:var finalDate = future.getDate() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+future.getFullYear();var finalDate2 = future2.getDate() +'-'+ ((future2.getMonth() + 1) < 10 ? '0' : '') + (future2.getMonth() + 1) +'-'+ future.getFullYear();然后会得到这样的输出<head>&nbsp; <meta charset="utf-8">&nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; <title>jQuery UI Datepicker - Default functionality</title>&nbsp; <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">&nbsp; <link rel="stylesheet" href="/resources/demos/style.css">&nbsp; <script src="https://code.jquery.com/jquery-1.12.4.js"></script>&nbsp; <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>&nbsp; <script>&nbsp; $( function() {&nbsp; &nbsp; $( "#datepicker" ).datepicker({&nbsp; &nbsp; &nbsp; &nbsp; dateFormat: 'dd/mm/yy'&nbsp; &nbsp; });&nbsp; });&nbsp; </script>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;function myfunction(){&nbsp; &nbsp; var future = new Date(document.getElementById("datepicker").value); // get today datefuture.setDate(future.getDate() + 7); // add 7 days&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var finalDate = future.getDate() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+future.getFullYear();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var future2 = new Date(document.getElementById("datepicker").value);&nbsp; &nbsp; future2.setDate(future2.getDate() + 10); // add 7 days&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; var finalDate2 = future2.getDate() +'-'+ ((future2.getMonth() + 1) < 10 ? '0' : '') + (future2.getMonth() + 1) +'-'+ future.getFullYear();&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; alert('Your order will be printed on ' + finalDate&nbsp; + '\nYou should recieve your order ' + finalDate2);}&nbsp; &nbsp; </script></head><body>&nbsp;<form onSubmit="myfunction()"><p>Date: <input type="text" id="datepicker" name="date"></p>&nbsp;<input type="submit" lable="Submit">&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp;<p id="demo"></p>&nbsp; &nbsp; </form>&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;</body></html>更新将输入格式更改为 DD/MM/YYYY改变了$( "#datepicker" ).datepicker();到$( "#datepicker" ).datepicker({&nbsp; &nbsp;dateFormat: 'dd/mm/yy'});
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答