Warm tip: This article is reproduced from serverfault.com, please click

javascript-将日期格式从MM / DD / YYYY修改为DD / MM / YYY

(javascript - Edit date format from MM/DD/YYYY to DD/MM/YYY)

发布于 2020-11-29 11:46:51

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

唯一的问题是将其拆分为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>
Questioner
Chris James Fewster
Viewed
11
Adhitya 2020-11-29 20:34:42

我已在你的代码上以日期格式进行了编辑:var finalDatevar 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>
  <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({
        dateFormat: 'dd/mm/yy'
    });
  });
  </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.getDate() +'-'+ ((future.getMonth() + 1) < 10 ? '0' : '') + (future.getMonth() + 1) +'-'+future.getFullYear();
    
    var future2 = new Date(document.getElementById("datepicker").value);
    future2.setDate(future2.getDate() + 10); // add 7 days
    
    var finalDate2 = future2.getDate() +'-'+ ((future2.getMonth() + 1) < 10 ? '0' : '') + (future2.getMonth() + 1) +'-'+ future.getFullYear();
    
        

    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>

更新

将输入格式更改为DD / MM / YYYY

已变更

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

$( "#datepicker" ).datepicker({
   dateFormat: 'dd/mm/yy'
});