比较C#中的日期

本文关键字:日期 比较 | 更新日期: 2023-09-27 18:26:51

我是C#和asp.net的初级程序员。我正忙于创建一个Hotel应用程序。例如,某人可以预订8天的假期。但现在我需要添加一个计算价格的公式。我写的方法是从数据库中获取房间的每晚价格。该人停留的天数被输入到视图中,并传递给控制器。所以我想计算控制器内部的价格。但现在我遇到了一个问题,因为旺季入住酒店的价格比淡季高。所以价格每天都不一样。但现在我真的不知道如何比较日期,这样我就能给出准确的总价。

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

如果我能成功地创造出每天不同的价格,那么最后一件事就不会那么难了。每个日期的价格应该相互相加,这样我就有了总价。

所以我的问题是:

  • 比较日期的最佳方式是时间跨度吗
  • 有没有一种简单的方法来计算这个?我不喜欢固定的日期
  • 有什么好的教程吗

比较C#中的日期

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

这对你来说可能有些过头了,但我会将不同的"季节"及其费率封装在一个类中,并向该类添加一个方法,以确定日期是否在该"季节"内。这将简化其他方法。

然后我会创建一个方法,在给定一个日期的情况下,返回该日期的利率。

最后,我将通过调用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);