比较C#中的日期

时间:2014-10-17 17:07:20

标签: c# asp.net-mvc-4 date compare

我是C#和asp.net的初级程序员。我正忙着创建一个酒店应用程序。有人可以预订例如8天的假期。但现在我需要添加一个计算价格的公式。我写的方法是从数据库中获取每晚房间的价格。此人留下的日子进入视图并传递给控制器​​。所以我想计算控制器内的价格。但现在我有一个问题,因为入住酒店的价格在旺季高于淡季。所以价格每天都不同。但现在我现在还没有如何比较日期,所以我能够给出准确的总价。

我查看了堆栈溢出的一些线程,他们经常建议使用Timespan来比较日期。但我想知道Timespan对我来说是最好的解决方案吗?我的项目的原因价格应该流动而不是固定。例如,它不应该像5月28日 - 7月10日是每晚120欧元,但更像是5月28日€109,5月29日€112,5月30日€113 - 7月9日€127,7月10日130.

如果我能够每天创造不同的价格,那么我希望最后一件事情不应该那么难。每个日期的价格应该相互加起来,所以我将得到总价。

所以我的问题是:

  • 是比较日期Timespan的最佳方式吗?
  • 有一种简单的方法可以计算出来吗?我不喜欢固定日期。
  • 有没有这方面的好教程?

1 个答案:

答案 0 :(得分:1)

我只是比较开始日期和结束日期之间的每个Date对象,看看它是否在定义的范围内以确定比率,并在我去的时候总结它们。

这对你来说可能有点过头了,但我会把不同的季节包括在内。和他们在课堂上的费率,并在课堂上添加一种方法,以确定某个日期是否属于该季节。#39;这将简化其他方法。

然后我会创建一个方法,给定一个日期,将返回该日期的费率。

最后,我会通过在客户的开始日期(含)和结束日期(不包括)之间的每一天调用GetRate()方法来计算总价。

以下是我将如何做的示例。首先,举办“赛季”的班级

public class Season
{
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public int Rate { get; set; }

    public bool ContainsDate(DateTime date)
    {
        // Assumption: Year is ignored - seasons are considered 
        //             to start and end on the same date each year
        //
        // Rules: (remember a season may start in Dec and end in Jan,
        //         so you cant just check if the date is greater than
        //         the start or less than the end!)
        // 
        // 1. If the start and end month are the same,
        //    return true if the month is equal to start (or end) month
        //    AND the day is between start and end days.
        // 2. If the date is in the same month as the start month, 
        //    return true if the day is greater than or equal to start day.
        // 3. If the date is in the same month as the end month, 
        //    return true if the day is less than or equal to end day.
        // 4. If the StartMonth is less than the EndMonth, 
        //    return true if the month is between them.
        // 5. Otherwise, return true if month is NOT between them.

        if (StartDate.Month == EndDate.Month)
            return date.Month == StartDate.Month &&
                   date.Day >= StartDate.Day &&
                   date.Day <= EndDate.Day;

        if (date.Month == StartDate.Month)
            return date.Day >= StartDate.Day;

        if (date.Month == EndDate.Month)
            return date.Day <= EndDate.Day;

        if (StartDate.Month <= EndDate.Month)
            return date.Month > StartDate.Month && date.Month < EndDate.Month;

        return date.Month < EndDate.Month || date.Month > StartDate.Month;
    }
}

接下来,计算特定日期的费率的方法:

public static int GetRate(DateTime date)
{
    // Normally these 'seasons' and rates would not be hard coded here
    const int offSeasonRate = 125;

    var winterSeason = new Season
    {
        StartDate = DateTime.Parse("November 15"), 
        EndDate = DateTime.Parse("January 12"), 
        Rate = 150
    };

    var springSeason = new Season
    {
        StartDate = DateTime.Parse("May 20"), 
        EndDate = DateTime.Parse("June 15"), 
        Rate = 140
    };

    var summerSeason = new Season
    {
        StartDate = DateTime.Parse("July 10"), 
        EndDate = DateTime.Parse("August 31"), 
        Rate = 170
    };

    // Create a list of all the seasons
    var seasons = new List<Season> {winterSeason, springSeason, summerSeason};

    // Loop through all the seasons and see if this date is in one of them
    foreach (var season in seasons)
    {
        if (season.ContainsDate(date))
        {
            // Note: depending on your implementation, Rate could be a multiplier
            // in which case you would return (offSeasonRate * season.Rate);
            return season.Rate;
        }
    }

    // If we get this far, the date was not in a 'season'
    return offSeasonRate;
}

最后,这是获取日期范围总价格的方法:

var startDate = DateTime.Today;
var endDate = startDate.AddDays(2);
var price = 0;

// Sum the rates for each day between 
// start date (inclusive) and end date (exclusive).
for (var curDate = startDate; curDate < endDate; curDate = curDate.AddDays(1))
{
    price += GetRate(curDate);
}

Console.WriteLine("The total cost from {0} to {1} is: €{2}", 
    startDate, endDate, price);