0

This is Robert and I've got some troubles with JavaScript.

I got an <input value="DATE" hidden=""> (which is hidden). The only thing I want to ask you is: I want to check if the date in <input value=""> is passed. If it is passed I want to change the background color of the container in CSS. Unfortunately I don't know how to do this.

If it's possible to convert the input value e.G value="03/08/2017" automatically to <div class="tag">AUG</div> and <div class="monat">03</div>. So if I update the value through my CMS I want to update the 03/AUG also. So if value="28/12/2017" the result schould be automatically DEZ and 28

I hope you could follow me. Cheers.

<script type="text/javascript"> 
     $("tr").sort(function(a,b){
      String date = new Date($(a).find("input").val()).getDate();
      if(date < Date.now){
        //Change the background color of eventscontainer to red
      }    
    }).appendTo("tbody")
</script>
<table border="0">
            <thead>
               
      </thead>
          <tbody>
               
               <!-- SPALTE 1-->
           
            <tr>
                  
      <td>
                   <input value="03/08/2017">
                   <div class="tag">AUG&nbsp;</div>
                   <br/>
                   <div class="monat">03&nbsp;</div>
              </td> 
              <td class="info">
                    <div class="information">
                    
                    <img src="musik1.png" width="20em" height="20em" style="margin-bottom: 1%;">
                    Charts, EDM, HipHop
                    
      <br/> <img src="location.png" width="20em" height="20em" style="margin-bottom: 1%;">
                   Wien, Österreich<br/>
                    
                     <img src="dj.png" width="25em" height="25em" style="margin-bottom: 1%;">
                    Club Ypsilon
      
       </div>
                   
              </td>              
              <td class="tag-ausgeschrieben"><br/>
              fr&nbsp;&nbsp;</td>
                   
            </tr>     
          </tbody>
    </table>
Ushma Joshi
  • 459
  • 6
  • 19
Robert
  • 38
  • 1
  • 5
  • Use Below code for check date –  Jul 21 '17 at 11:11
  • the better way is to check it via regexpressions because if you put a month 13 in a Date object, you will get a date with month 1 in the following year. never trust in user inputs. but for the leap year problem Date object is prefect because it make it all automatic and gives you back the first of march if 28 feb in the given year does not exist – mtizziani Jul 21 '17 at 11:12

2 Answers2

1

getDate will return the day of the month. You should use getTime which will return the number of milliseconds since 1970/01/01.

var date = new Date("03/08/2017");
if(date.getTime() < Date.now()) {
   console.log("The date has allready passed");
}

Or you can just compare the Date objects.

var date = new Date("03/08/2017");
if(date < Date.now()) {
   console.log("The date has allready passed");
}

More information on when you can directly use the Date type for comparisons and when you need to call getTime can be found here.

NtFreX
  • 10,379
  • 2
  • 43
  • 63
0

Try and use the following JS:

var selectedText = $(a).find("input").val();
var selectedDate = new Date(selectedText);
var now = new Date();
 if (selectedDate < now) {
    alert("Date must be in the future");
}
Joe
  • 4,877
  • 5
  • 30
  • 51