确定DateTime是否在给定的日期范围内

cha*_*ara 1 c# datetime

Aries          March 21 to April 20.
Taurus         April 21 to May 20.
Gemini         May 21 to June 21.
Run Code Online (Sandbox Code Playgroud)

我需要通过将用户的出生月份和日期作为输入来打印用户的占星符号.我如何获得日期范围?

EX:3月21日至4月20日

Ian*_*cer 9

您实际上不需要构建日期时间范围来解决此问题.一个基于月份的简单switch语句,每个月返回一个简单的if语句,返回两个星号中的一个就足够了.

   e.g
     switch (month)
     {
       case 1:
          if (day <20) return "Capricorn"; else return "Aquarius";
          break;
       case 2:
          ...
Run Code Online (Sandbox Code Playgroud)


Ate*_*eik 6

这是方法的实现:

    private string GetHoroName(DateTime dt)
    {
        int month = dt.Month;
        int day = dt.Day;
        switch (month)
        {
            case 1:
                if (day <= 19)
                    return "Capricorn";
                else
                    return "Aquarius";

            case 2:
                if (day <= 18)
                    return "Aquarius";
                else
                    return "Pisces";
            case 3:
                if (day <= 20)
                    return "Pisces";
                else
                    return "Aries";
            case 4:
                if (day <= 19)
                    return "Aries";
                else
                    return "Taurus";
            case 5:
                if (day <= 20)
                    return "Taurus";
                else
                    return "Gemini";
            case 6:
                if (day <= 20)
                    return "Gemini";
                else
                    return "Cancer";
            case 7:
                if (day <= 22)
                    return "Cancer";
                else
                    return "Leo";
            case 8:
                if (day <= 22)
                    return "Leo";
                else
                    return "Virgo";
            case 9:
                if (day <= 22)
                    return "Virgo";
                else
                    return "Libra";
            case 10:
                if (day <= 22)
                    return "Libra";
                else
                    return "Scorpio";
            case 11:
                if (day <= 21)
                    return "Scorpio";
                else
                    return "Sagittarius";
            case 12:
                if (day <= 21)
                    return "Sagittarius";
                else
                    return "Capricorn";
        }
        return "";
    }
Run Code Online (Sandbox Code Playgroud)