0

Sorry for my errors but i'm very noob :D I have to set min date in an input in HTML with a IF: IF today is > 01-06-2019 set min to today ELSE set min to 01-06-2019 Can anybody help me? This is my code:

function setMin1() {
 var varData = new Date('01-06-2019'); //dd-mm-YYYY
 var today = new Date();
 today.setHours(0,0,0,0);

 if(varData >= today) {
  document.getElementById('set').min = '01-06-2019';
 }
 else {
  document.getElementById('set').min = '<?php echo date('d-m-Y');?>';
 }
}
Marco Parisi
  • 5
  • 1
  • 3
  • Note that '01-06-2019' is not a format supported by ECMAScript and returns an invalid date in some implementations. See [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). – RobG Feb 06 '19 at 23:33

3 Answers3

0

I think what you are looking for is the setAttribute method:

document.getElementById('set').setAttribute('min', '01-06-2019');
RobG
  • 142,382
  • 31
  • 172
  • 209
IamMowgoud
  • 308
  • 4
  • 13
  • 1
    That appears to be an unfortunate munge of jQuery and javascript. [*DOM elements*](https://developer.mozilla.org/en-US/docs/Web/API/Element) don't have an *attr* method. If you want to set the value of an attribute, use [*setAttribute*](https://developer.mozilla.org/en-US/docs/Web/API/Element/setAttribute): `document.getElementById('set').setAttribute('min', '01-06-2019')`. – RobG Feb 06 '19 at 23:22
0

It won't work because your passing a string to your input. Your input type's number. You can use data-type instead.

mohiris
  • 50
  • 1
  • 6
  • Can u explain me how to do this? – Marco Parisi Feb 06 '19 at 12:33
  • First change your input type from number to date: and set a min, max attribute with the correct format (Y-m-d). See more here https://developer.mozilla.org/fr/docs/Web/HTML/Element/Input/date – mohiris Feb 06 '19 at 12:47
  • You should include that comment in the answer. – RobG Feb 06 '19 at 23:25
  • "*Your input type's number*". How do you know that? The OP doesn't say what the input type is, I'd expect *date*. The values for an input type date should be in YYYY-MM-DD (aka "ISO") format. – RobG Feb 06 '19 at 23:36
0

I guess the input is type date. You can set the minimum value using the min attribute, e.g.

<input type="date" name="someDate" min="2019-01-01">

Note that in HTML, an ISO 8601 formatted date will be treated as local, but when parsed by the built–in ECMAScript parser will be treated as UTC.

If you set the value using script, you're relying on the host system's clock being accurate. The value can be set using setAttribute:

window.onload = function(){

  var d = new Date();

  // Build ISO 8601 format date string
  var s = d.getFullYear() + '-' + 
          ('0' + (d.getMonth()+1)).slice(-2) + '-' +
          ('0' + d.getDate()).slice(-2);

  // Set the value of the value and min attributes
  var node = document.querySelector('input');
  if (node) {
    node.setAttribute('min', s);
    node.setAttribute('value', s);
  }
}
<form onsubmit="return false;">
  <input type="date"><br>
  <input type="reset">
</form>
RobG
  • 142,382
  • 31
  • 172
  • 209