【问题标题】:Comparing dates in C#在 C# 中比较日期
【发布时间】:2014-10-17 17:07:20
【问题描述】:

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

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

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

所以我的问题是:

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

【问题讨论】:

  • 只知道您的季节的开始和结束日期,并检查您的预订日期是否在该范围内,例如if(bookingDate >= seasonStartDate && bookingDate <= seasonEndDate)
  • 您打算如何存储价格?我建议在数据库中,然后您可以编写一个查询,根据开始和结束日期计算价格。
  • 我会逐一获取每天的价格,然后将它们相加。这比处理时间跨度和边缘情况要简单得多,而且由于只有几天时间,因此对性能的影响是微不足道的。

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


【解决方案1】:

我只是比较开始日期和结束日期之间的每个 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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-15
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多