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

Edit date format from MM/DD/YYYY to DD/MM/YYY

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

so I have written some code that will allow someone to enter their order date and will return expected printing and delivery dates.

Only issue is its splitting out MM/DD/YYYY. Any help to get this working is much appreciated.

<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
0
Adhitya 2020-11-29 20:34:42

I'm edited in date format on your code: var finalDate and var finalDate2. Because in the code make a format date in your jquery.

In code below will changed format date to DD/MM/YYYY
So I changed to this code:

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();

Then will get output like this

<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>

UPDATE

For Changed Input Format to DD/MM/YYYY

Changed

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

To

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