Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are three distinct subproblems here. First, parse the input in dd/mm/yy format into an object suitable for doing the date arithmetic. Do the +60 days arithmetic. Format the result in dd/mm/Y format.</p> <p>Javascript has a Date object which is fine for doing the simple arithmetic you need, in my opinion. Adding 60 days to a date is as easy as</p> <pre><code>givenDate.setDate(givenDate.getDate() + 60) </code></pre> <p>The setDate method takes care of out of range values, see</p> <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate</a></p> <p>For parsing you can extract year, month, and day from your string in the dd/mm/yy format (basically using the string split() method, and feeding the three values to the Date constructor, as in</p> <pre><code>new Date(year, month, day) </code></pre> <p>As for formatting, you can use the Date object getter functions to build up your string. See</p> <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date" rel="nofollow">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date</a></p> <p>Full code:</p> <pre><code>function parse(dateStr) { var parts = dateStr.split('/').map(function (digits) { // Beware of leading zeroes... return parseInt(digits, 10); }); return new Date(1900 + parts[2], parts[1], parts[0]); } function format(date) { var parts = [ date.getDate(), date.getMonth(), date.getYear() + 1900 ]; if (parts[0] &lt; 10) { parts[0] = '0' + parts[0]; } if (parts[1] &lt; 10) { parts[1] = '0' + parts[1]; } return parts.join('/'); } function add(date, days) { date.setDate(date.getDate() + days); return date; } console.log(format(add(parse('11/06/83'), 60))); // Outputs: 09/08/1983 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload