C# 用C语言比较日期#
我是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欧元 如果我能成功地创造一个不同的价格每天那么最后一件事不应该那么难,我希望。每个日期的价格应该加在一起,这样我就有了总价 因此,我的问题是:C# 用C语言比较日期#,c#,asp.net-mvc-4,date,compare,C#,Asp.net Mvc 4,Date,Compare,我是C#和asp.net的初级程序员。我正忙于创建一个酒店应用程序。例如,某人可以预订8天的假期。但是现在我需要添加一个计算价格的公式。我写的方法是从数据库中获取每晚的房价。在视图中输入此人停留的天数,并将其传递给控制器。因此,我想计算控制器中的价格。但是现在我遇到了一个问题,因为在旺季入住酒店的价格要高于淡季。所以价格每天都不一样。但现在我真的不知道如何比较日期,所以我能够给出一个准确的总价 我看过一些关于堆栈溢出的线程,它们经常建议使用Timespan来比较日期。但我想知道Timespan对
- 比较日期和时间跨度的最佳方法是什么
- 有没有一个简单的计算方法?我不想要固定的日期
- 这方面有什么好的教程吗
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;
}
}
公共课季
{
公共日期时间起始日期{get;set;}
公共日期时间结束日期{get;set;}
公共整数速率{get;set;}
公共bool ContainsDate(日期时间日期)
{
//假设:忽略年份-考虑季节
//每年在同一日期开始和结束
//
//规则:(记住一个赛季可能从12月开始,到1月结束,
//因此,您不能只检查日期是否大于
//开始或小于结束!)
//
//1.如果开始月份和结束月份相同,
//如果月份等于开始(或结束)月份,则返回true
//这一天介于开始日和结束日之间。
//2.如果日期与起始月在同一个月内,
//如果日期大于或等于开始日期,则返回true。
//3.如果日期与月末在同一个月内,
//如果日期小于或等于结束日期,则返回true。
//4.如果开始月份小于结束月份,
//如果月份介于两者之间,则返回true。
//5.否则,如果月份不在两者之间,则返回true。
如果(StartDate.Month==EndDate.Month)
返回日期.月份==开始日期.月份&&
日期>=开始日期&&
date.Day=开始日期.Day;
如果(date.Month==EndDate.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;
}
publicstaticintgetrate(DateTime日期)
{
//通常,这些“季节”和费率不会在此处硬编码
常数int offSeasonRate=125;
冬季=新季节
{
StartDate=DateTime.Parse(“11月15日”),
EndDate=DateTime.Parse(“1月12日”),
费率=150
};
var springSeason=新赛季
{
StartDate=DateTime.Parse(“5月20日”),
EndDate=DateTime.Parse(“6月15日”),
费率=140
};
var summerSeason=新赛季
{
StartDate=DateTime.Parse(“7月10日”),
EndDate=DateTime.Parse(“8月31日”),
费率=170
};
//创建所有季节的列表
var seasons=新列表{冬季、春季、夏季};
//循环浏览所有季节,看看这个日期是否在其中一个季节
foreach(季节中的变量季节)
{
if(季节日期)
{
//注意:根据您的实现,速率可能是一个乘数
//在这种情况下,您将返回(淡季费率*季节费率);
返航季节.费率;
}
}
//如果我们走到这一步,日期就不是“季节”
休季收益率;
}
最后,以下是获取日期范围总价的方法:
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);
var startDate=DateTime.Today;
var endDate=开始日期。添加日期(2);
var价格=0;
//将两个月之间每天的费率相加
//开始日期(含)和结束日期(不含)。
对于(变量curDate=startDate;curDate
只需确定赛季的开始和结束日期,并检查您的预订日期是否在该范围内,如如果(bookingDate>=季节开始日期和&bookingDate您计划如何存储价格?我建议使用DB,然后您可以编写一个查询,根据开始日期和结束日期计算价格。我会逐个获取每天的价格,然后将其相加。这比处理时间跨度和边缘案例要简单得多