Finding astrological signs based on birthdates using JavaScript

We are required to write a JavaScript function that takes in a date object and returns the astrological sign related to that birthdate based on zodiac date ranges.

Understanding Zodiac Signs

Each zodiac sign corresponds to specific date ranges throughout the year. The challenge is handling the transition dates correctly, especially for signs that span across months.

Example

Following is the code:

const date = new Date();
// as on 2 April 2021
const findSign = (date) => {
    const days = [21, 20, 21, 21, 22, 22, 23, 24, 24, 24, 23, 22];
    const signs = ["Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn"];
    let month = date.getMonth();
    let day = date.getDate();
    if(month == 0 && day <= 20){
        month = 11;
    }else if(day < days[month]){
        month--;
    };
    return signs[month];
};
console.log(findSign(date));
Aries

The output may vary based on the time we are running the function because we are using current date.

How It Works

The function uses two arrays: days contains the starting day for each zodiac sign, and signs contains the corresponding sign names. The logic handles month transitions by checking if the current day falls before the sign's start date.

Testing with Specific Dates

// Test with specific birthdates
console.log(findSign(new Date(1990, 0, 15)));  // January 15
console.log(findSign(new Date(1990, 6, 25)));  // July 25
console.log(findSign(new Date(1990, 11, 25))); // December 25
Capricorn
Leo
Capricorn

Enhanced Version with Date Range Display

const getZodiacSign = (birthDate) => {
    const zodiacSigns = [
        { sign: "Capricorn", start: [12, 22], end: [1, 19] },
        { sign: "Aquarius", start: [1, 20], end: [2, 18] },
        { sign: "Pisces", start: [2, 19], end: [3, 20] },
        { sign: "Aries", start: [3, 21], end: [4, 19] },
        { sign: "Taurus", start: [4, 20], end: [5, 20] },
        { sign: "Gemini", start: [5, 21], end: [6, 20] },
        { sign: "Cancer", start: [6, 21], end: [7, 22] },
        { sign: "Leo", start: [7, 23], end: [8, 22] },
        { sign: "Virgo", start: [8, 23], end: [9, 22] },
        { sign: "Libra", start: [9, 23], end: [10, 22] },
        { sign: "Scorpio", start: [10, 23], end: [11, 21] },
        { sign: "Sagittarius", start: [11, 22], end: [12, 21] }
    ];
    
    const month = birthDate.getMonth() + 1; // JavaScript months are 0-indexed
    const day = birthDate.getDate();
    
    for (let zodiac of zodiacSigns) {
        const [startMonth, startDay] = zodiac.start;
        const [endMonth, endDay] = zodiac.end;
        
        if ((month === startMonth && day >= startDay) || 
            (month === endMonth && day <= endDay)) {
            return zodiac.sign;
        }
    }
};

// Test the enhanced version
console.log(getZodiacSign(new Date(1990, 2, 15)));  // March 15
console.log(getZodiacSign(new Date(1990, 7, 10)));  // August 10
Pisces
Leo

Conclusion

Both approaches effectively determine zodiac signs from birthdates. The first method uses arrays for compact code, while the enhanced version provides clearer date range logic and easier maintenance.

Updated on: 2026-03-15T23:19:00+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements