簡體   English   中英

確定DateTime是否在給定的日期范圍內

[英]Determine if DateTime is in a given Date Range

Aries          March 21 to April 20.
Taurus         April 21 to May 20.
Gemini         May 21 to June 21.

我需要通過獲取用戶的出生月份和日期作為輸入來打印用戶的占星符號。 如何獲取日期范圍?

例:3月21日至4月20日

您實際上不需要構造日期時間范圍來解決此問題。 一個基於月份的簡單switch語句就足夠了,每個月都有一個簡單的if語句,該語句返回兩個星號之一。

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

這是該方法的實現:

    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 "";
    }

創建一個描述星號的類...

class StarSign
{
    public string Name {get;set;}

    public DateTime StartDate {get;set;}

    public DateTime EndDate {get;set;}

    public bool CoversDate(DateTime birthday)
    {
        return birthday <= this.EndDate && birthday >= this.StartDate;
    }
}

我知道我錯過了獲得公認答案的機會,但是在給柯克·布羅德赫斯特(Kirk Broadhurst)抨擊之后,我認為我最好提供自己的答案。

我對問題的理解chamara需要這樣的東西:

var birthDate = new DateTime(1923, 4, 20);
var starSign = StarSigns.GetFor(birthDate);

Console.WriteLine(starSign); // Taurus (April 20 - May 20)
Console.WriteLine(starSign.GetStartDate(2010)); // 2010/04/20 00:00:00
Console.WriteLine(starSign.GetEndDate(2010)); // 2010/05/20 23:59:59

var starSign1 = StarSigns.GetFor(10, 22);
var starSign2 = StarSigns.GetFor(10, 23);

Console.WriteLine(starSign1); // Libra (September 23 - October 22)
Console.WriteLine(starSign2); // Scorpio (October 23 - November 21)

所以這是我的課程:

public static class StarSigns
{
    private static StarSign[] _starSigns;

    static StarSigns()
    {
        var names = new[]
        {
            "Aquarius", "Pisces", "Aries", "Taurus",
            "Gemini", "Cancer", "Leo", "Virgo",
            "Libra", "Scorpio", "Sagittarius", "Capricorn", 
        };

        var days = new[]
        {
            20, 18, 20, 20,
            21, 21, 22, 23,
            23, 23, 22, 22,
        };

        _starSigns = (from i in Enumerable.Range(0, 12)
                      let name = names[i]
                      let startMonth = i + 1
                      let startDay = days[i]
                      let endDay = days[(i + 1) % 12] - 1
                      select new StarSign(name, startMonth, startDay, endDay)).ToArray();
    }

    public static StarSign GetFor(DateTime birthDate)
    {
        return (from starSign in _starSigns
                let startDate = starSign.GetStartDate(birthDate.Year)
                let endDate = starSign.GetEndDate(birthDate.Year)
                where startDate <= birthDate
                where endDate >= birthDate
                select starSign).Single();
    }

    public static StarSign GetFor(int birthMonth, int birthDay)
    {
        return GetFor(new DateTime(2010, birthMonth, birthDay));
    }

}

public class StarSign
{
    public StarSign(string name, int startMonth, int startDay, int endDay)
    {
        this.Name = name;
        this.StartMonth = startMonth;
        this.StartDay = startDay;
        this.EndDay = endDay;
    }

    public string Name { get; private set; }

    public int StartDay { get; private set; }
    public int EndDay { get; private set; }

    public int StartMonth { get; private set; }

    public int EndMonth
    {
        get
        {
            return this.StartMonth + 1;
        }
    }

    public DateTime GetStartDate(int year)
    {
        return new DateTime(year, this.StartMonth, this.StartDay);
    }

    public DateTime GetEndDate(int year)
    {
        var nextStart = new DateTime(year, (this.StartMonth % 12) + 1, this.EndDay + 1);
        return nextStart.Subtract(TimeSpan.FromTicks(1));
    }

    public string Period
    {
        get
        {
            var startDate = this.GetStartDate(2010);
            var endDate = this.GetEndDate(2010);
            var template = "{0:MMMM dd} - {1:MMMM dd}";
            return String.Format(template, startDate, endDate);
        }
    }

    public override string ToString()
    {
        return String.Format("{0} ({1})", this.Name, this.Period);
    }
}

請享用!

還沒有測試過,但是看起來不錯... :)

    private static string GetStarSign(DateTime dateOfBirth)
    {
        int[] fromDate = { 21, 20, 21, 21, 22, 22, 23, 23, 22, 23, 22, 22 };
        string[] signs = { "Capricorn", "Aquarius", "Pisces", "Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn" };
        if (dateOfBirth.Day < fromDate[dateOfBirth.Month - 1])
            return signs[dateOfBirth.Month - 1];
        return signs[dateOfBirth.Month];
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM