在C#中,
我们如何在周日查看某个日期?
例如:2014年6月2日
当前周:02/02/2014 - 08/02/2014
所以这个日期是在上周......
答案 0 :(得分:2)
使用此选项进行检查(如果您希望始终从fromDate开始一周,则最后一个参数是可选的,您不需要使用最后一个参数):
public static bool DateInside(DateTime checkDate,
DateTime fromDate, DateTime? lastDate = null)
{
DateTime toDate = lastDate != null ? lastDate.Value : fromDate.AddDays(6d);
return checkDate >= fromDate && checkDate <= toDate;
}
致电使用:
bool isDateInside = DateInside(new DateTime(2014, 02, 06),
new DateTime(2014, 02, 02)); // return true
先搜索:)答案也在这里:How to check whether C# DateTime is within a range
如果你想检查日期是否在同一周内,那么你可以使用它:
public static bool DateInsideOneWeek(DateTime checkDate, DateTime referenceDate)
{
// get first day of week from your actual culture info,
DayOfWeek firstWeekDay = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek;
// or you can set exactly what you want: firstWeekDay = DayOfWeek.Monday;
// calculate first day of week from your reference date
DateTime startDateOfWeek = referenceDate;
while(startDateOfWeek.DayOfWeek != firstWeekDay)
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); }
// fist day of week is find, then find last day of reference week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d);
// and check if checkDate is inside this period
return checkDate >= startDateOfWeek && checkDate <= endDateOfWeek;
}
我的文化信息的实际周从2014年2月3日星期一开始(所以对我来说是2月3日到2月9日之间的一周)。如果我检查参考日期(第二个参数)的任何日期为今天(2014年2月06日),我得到这个结果:
For 2014-Feb-02 (Sunday before this week): false
For 2014-Feb-03 (Monday inside this week): true
For 2014-Feb-06 (Today inside this week): true
For 2014-Feb-09 (Sunday inside this week): true
For 2014-Feb-10 (Monday next week): false
您可以调用此方法来检查一个日期是否在同一周内作为参考,如下所示:
DateInsideOneWeek(new DateTime(2014, 02, 02), new DateTime(2014, 02, 06));
您可以使用以下代码找到当前周开始日期和结束日期:
DateTime startDateOfWeek = DateTime.Now.Date; // start with actual date
while(startDateOfWeek.DayOfWeek != DayOfWeek.Monday) // set first day of week in your country
{ startDateOfWeek = startDateOfWeek.AddDays(-1d); } // after this while loop you get first day of actual week
DateTime endDateOfWeek = startDateOfWeek.AddDays(6d); // you just find last week day
这是你想要的吗?
答案 1 :(得分:1)
嗯
public bool isBetween(DateTime input, DateTime date1, DateTime date2)
{
if (input > date1 && input < date2)
return true;
else
return false;
}
输入=您的日期 date1&amp; date2 =一周的开始和结束
答案 2 :(得分:0)
怎么样:
bool inRange = (date >= lowerDate && date <= upperDate);
答案 3 :(得分:0)
这是另一种解决方案:)
public static class DateExtensions
{
private static void Swap<T>(ref T one, ref T two)
{
var temp = one;
one = two;
two = temp;
}
public static bool IsFromSameWeek(this DateTime first, DateTime second, DayOfWeek firstDayOfWeek = DayOfWeek.Monday)
{
// sort dates
if (first > second)
{
Swap(ref first, ref second);
}
var daysDiff = (second - first).TotalDays;
if (daysDiff >= 7)
{
return false;
}
const int TotalDaysInWeek = 7;
var adjustedDayOfWeekFirst = (int)first.DayOfWeek + (first.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
var adjustedDayOfWeekSecond = (int)second.DayOfWeek + (second.DayOfWeek < firstDayOfWeek ? TotalDaysInWeek : 0);
return adjustedDayOfWeekSecond >= adjustedDayOfWeekFirst;
}
}
Upd:它的性能至少比@Atiris解决方案好两倍:)