ladyd
ladyd

Reputation: 37

how to compare between 2 dates without year? only month and day

I have a function that gets the birthdate( only day and month) and compares if its between 2 dates newsigndate is undefined.why? and also AriesFrom and AriesTo.

what to do?

I want to compare the birth date according to day and month for example is 21/3>21/2

yes

return Aries

thanks

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>

function yourSign() {
  var signDate = $("input[name='birthDate']").val();
  tempsignDate = signDate.split("-").join("/");
  newsignDate = tempsignDate.substring(5, 10);

  var AriesDateFrom = "21/03";
  var AriesDateTo = "20/04";

  var Ad1 = AriesDateFrom.split("/");
  var Ad2 = AriesDateTo.split("/");

  var s = newsignDate.split("/");
  //var date = new Date().getDay() + "/" + new Date().getMonth() + 1 

  var AriesFrom = new Date(Ad1[1], Ad2[2]);


  newsignDate = new Date(s[1], s[2]);

  if (newsignDate == "") {
    alert("enter birthday");

  } else if (newsignDate >= AriesFrom && newsignDate < AriesTo) {
    $("#output").val("Aries");

  }
}

</script>

Upvotes: 1

Views: 1489

Answers (3)

ladyd
ladyd

Reputation: 37

Thank you!@bhavesh ajani instead of alert I need that the output will be in the output field.

<script>
function yoursign()
 if (s >= Ad1 && s < Ad2) {
output = "Aries";       

$("#output").val(output);

</script>
<body>

birthdate

        <button onclick="yourSign()" id="send">send</button><br />


        <label>yoursign: </label><br />

        <input type="text" name="output" id="output" />

Upvotes: 0

Bhavesh Ajani
Bhavesh Ajani

Reputation: 1261

in your else if statement variables are replace with these js file code.. i think your requirement is fullfiled,. thank you.

app.js :

function yourSign() {

  var signDate = $("input[name='birthDate']").val();
  tempsignDate = signDate.split("-").join("/");
  newsignDate = tempsignDate.substring(5, 10);


  var AriesDateFrom = "03/21";
  var AriesDateTo = "04/20";

  var Ad1 = AriesDateFrom.split("/");
  var Ad2 = AriesDateTo.split("/");
  var s = newsignDate.split("/");

  if(newsignDate == "") {
    alert("enter birthday");

  } 
  else if(s >= Ad1 && s < Ad2) {
    //$("#output").val("Aries");
    alert("Aries");
  }
  else
  {
    alert("not between");
  }}

HTML file :

<form>
    <input type="date" name="birthDate">
    <input type="submit" name="submit" value="submit" onclick="yourSign();">
</form>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="app.js"></script>

Upvotes: 0

Mark
Mark

Reputation: 92450

Here's a tip that might help think about this a little differently:

Instead of thinking about a bunch of date ranges, just think about a list of the last dates of the range. Now you can go through that list and find the first one that is greater than or equal your date. (it will be easier to compare dates is they are in MMDD format).

One way to do this in practice is to use the find() function of an array to find the first date greater than your given date:

// list of signs and last cuttoff
let signs = [ [ 'Capricorn', '0119' ],
[ 'Aquarius', '0218' ],
[ 'Pisces', '0320' ],
[ 'Aries', '0420' ],
[ 'Taurus', '0521' ],
[ 'Gemini', '0621' ],
[ 'Cancer', '0722' ],
[ 'Leo', '0822' ],
[ 'Virgo', '0921' ],
[ 'Libra', '1022' ],
[ 'Scorpio', '1121' ],
[ 'Sagittarius', '1220' ],
[ 'Capricorn', '1231' ] ]


// Jan 1
console.log(signs.find(s => s[1] >= '0101'))
// March 10
console.log(signs.find(s => s[1] >= '0310'))
// Sept 29
console.log(signs.find(s => s[1] >= '0929'))
// Dec 31
console.log(signs.find(s => s[1] >= '1231'))

Now you just need to transform your input into MMDD format and extract the sign from the array returned by find()

Upvotes: 0

Related Questions