0

I've a form with fields .daysfromtoday and #datefromtoday.

I would like to enter a number of days in .daysfromtoday and update #datefromtoday with the date yyyy-mm-dd format. (I know that there are other posts about this subject, but, if possible, I would like someone helps me with code I've written, to learn more)

HTML:

<input type="text" class="daysfromtoday" />
<input type="text" id="datefromtoday" name="delay" />

javascript:

$(document).ready(function (){
$(".daysfromtoday").on('change', function(){//
    var waitdays = $( ".daysfromtoday" ).val(); //take value from field .daysfromtoday
    var enddate = new Date();
    enddate.setDate(enddate.getDate() + waitdays); 
    var yyyy = enddate.getFullYear().toString();
    var mm = (enddate.getMonth()+1).toString();
    var dd = enddate.getDate().toString();
    document.getElementById("datefromtoday").value = yyyy + '-'  + mm + '-'  + dd; //outuput      
 });  
});

Problems: (and solutions)

  1. I get weird results: today is 2016-02-26, if I enter 100, I get 2087-7-17

  2. The result format should have the 0 (zeros): 2016-03-08

user229044
  • 232,980
  • 40
  • 330
  • 338
codeispoetry
  • 373
  • 2
  • 13
  • I know it's not a proper answer to your question but this library is worth checking out, it makes dealing with dates much much easier : http://momentjs.com/ – parallaxis Feb 26 '16 at 21:08
  • 2
    Try `var waitdays = parseInt($( ".daysfromtoday" ).val());` try to use `parseInt()` for your input... – brso05 Feb 26 '16 at 21:10
  • @brso05 Thanks! Do you have any suggestion for points #2 and #3? Thanks! – codeispoetry Feb 26 '16 at 21:26
  • @codeispoetry `if(mm < 10){monthFormatted = "0" + mm;}else{monthFormatted = "" + mm;}` then use `monthFormatted` in your output instead of `mm`. Same for day...Also get rid of the `.toString()` don't convert it to a `String` -> `var mm = (enddate.getMonth()+1);` – brso05 Feb 26 '16 at 21:28
  • 3. `getElementsByClassName()` and `$(".whatever")` return a collection of elements...Maybe try this:`$(".daysfromtoday").on('change', function(){// var waitdays = $(this).val();`. `this` should be the element that triggered the `change` event. – brso05 Feb 26 '16 at 21:38
  • @brso Sorry, but the if else solution doesn't seem o work... at least I can't.... see my edited question for the solution I adopted. Thanks!!! – codeispoetry Feb 27 '16 at 05:05
  • Please don't edit answers into your questions. If you have an answer, post it below *as an answer*. – user229044 Feb 29 '16 at 20:14

1 Answers1

3

You probably add string instead of number to the date. Try using parseInt function:

...
enddate.setDate(enddate.getDate() + parseInt(waitdays, 10));
...
Nhor
  • 3,860
  • 6
  • 28
  • 41
  • Thanks Nhor I put the parsInt in the variable definition. Do you have any suggestion for points #2 and #3? Thanks! – codeispoetry Feb 26 '16 at 21:28
  • 2) Instead of using `getDate`, `getMonth` and `getFullYear` I suggest you used a `substring` of `toISOString` like this: `enddate.toISOString().substr(0, 10)` then you get `string` like for example `"2016-02-26"` – Nhor Feb 26 '16 at 21:36
  • 1
    3) Using `getElementsByClassName` will return an array of results, because there can possibly be more than 1 element for given class, so if you are 100% sure there is only one, refer to it as `document.getElementsByClassName("datefromtoday")[0]` – Nhor Feb 26 '16 at 21:38
  • Thanks for #3. #2 works, but not for me, because it gives me the UTC, different from my time. Thanks! – codeispoetry Feb 27 '16 at 05:02