Home > Software design >  How can i compare to dates (string) in Javascript?
How can i compare to dates (string) in Javascript?

Time:07-27

I am writing function with condition, which should compare dates and in the end i should get special result for specific date? But it's not work right and i want to understand why? Can you help to me with it?

function yourDates(date){
if (date <= '20.04' && date >= '21.03'){
    zodiak_name = 'Aries';
}
else if (date <= '21.04' && date >= '21.05'){
    zodiak_name = 'Taurus';
}
else if (date <= '22.05' && date >= '21.06'){
    zodiak_name = 'Twins';
}
else if (date <= '22.06' && date >= '22.07'){
    zodiak_name = 'Cancer';
}
else if (date <= '23.07' && date >= '21.08'){
    zodiak_name = 'Lion';
}
else if (date <= '22.08' && date >= '23.09'){
    zodiak_name = 'Virgin';
}
else if (date <= '24.09' && date >= '23.10'){
    zodiak_name = 'Scale';
}
else if (date <= '24.10' && date >= '22.11'){
    zodiak_name = 'Scorpion';
}
else if (date <= '23.11' && date >= '22.12'){
    zodiak_name = 'Archer';
}
else if (date <= '23.12' && date >= '20.01'){
    zodiak_name = 'Capricorn';
}
else if (date <= '21.01' && date >= '19.02'){
    zodiak_name = 'Aquarius';
}
else {
    zodiak_name = 'Fish'
};

return zodiak_name

}

CodePudding user response:

Alghoritm for comparing DD.MM dates

  1. Compare months

  2. If months are equal, compare days

In this code you are treating strings like numbers, thus getting errors

But if you write dates in MM.DD format you can numberify and run usual comparement on them

CodePudding user response:

You can either use a library like moment.js or do something like mentioned :

var dateFrom = "06/06/2022";
var dateTo = "06/10/2022";
var dateToCheck = "06/08/2022";

var d1 = dateFrom.split("/");
var d2 = dateTo.split("/");
var c = dateToCheck.split("/");

var from = new Date(d1[2], parseInt(d1[1])-1, d1[0]);  // -1 because months are from 0 to 11
var to   = new Date(d2[2], parseInt(d2[1])-1, d2[0]);
var check = new Date(c[2], parseInt(c[1])-1, c[0]);

console.log(check > from && check < to)

CodePudding user response:

Instead of using hardcoded dates u can use index to find months and for days you can store them in array.

Thought Process

If month is January and the date is less than 20 it means we are still in december month's sign, In second condition we are checking if day is less than given day in days array for same month, which means we are still in previous month's sign Other than that we can just return the sign of current month

function getSign(userDate){
    const days =  [21, 20,21, 21, 22, 22, 23, 24, 24, 24, 23, 22];
    const signs = ["Aquarius", "Fish","Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"];
    let [day, month]=userDate.split(".").map(item=>parseInt(item));
    // Subtracting one cause index starts from 0
    // January is 0th month
    month--;
    
    if(month == 0 && day <= 20){
      month = 11;
    }else if(day < days[month]){
      month--;
    };
    return signs[month];
}

  • Related