C# 如何检查当天是否为工作日
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11495008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check if current day is working day
提问by Nemanja Boric
I have application that needs to be run on working days, and within working hours.
我有需要在工作日和工作时间内运行的应用程序。
In application configuration, I've set start time in format
在应用程序配置中,我以格式设置了开始时间
Monday-Friday
9:00AM-5:30PM
Now, I have a problem how to check if current day is within day boundare is (for the time is easy - parse time with DateTime.ParseExactand simple branch will do), but I don't know how to parse days.
现在,我有一个问题,如何检查当前日期是否在天边界内(因为时间很容易 - 使用DateTime.ParseExact简单的分支解析时间),但我不知道如何解析天数。
I've tried with:
我试过:
DayOfWeek day = DateTime.Now.DayOfWeek;
if (day >= (DayOfWeek)Enum.Parse(typeof(DayOfWeek), sr.start_day) &&
day <= (DayOfWeek)Enum.Parse(typeof(DayOfWeek), sr.end_day))
{ /* OK */ }
sr.start_dayand sr.end_dayare strings
sr.start_day并且sr.end_day是字符串
but the problem occurred during weekend testing - apparently, in DayOfWeekenum, Sunday is first day of the week (refering to the comments on MSDN page
但问题发生在周末测试 - 显然,在DayOfWeekenum,星期日是一周的第一天(参考MSDN 页面上的评论
I suppose I could do some gymnastics with current code, but I am looking for the most readable code available.
我想我可以用当前的代码做一些体操,但我正在寻找最易读的代码。
EditSorry for the misunderstanding - working days are not from Monday to Friday - they are defined as strings in config file, and they can be even from Friday to Saturday - which breaks my original code.
编辑抱歉误解 - 工作日不是从星期一到星期五 - 它们在配置文件中被定义为字符串,它们甚至可以从星期五到星期六 - 这打破了我的原始代码。
采纳答案by Nemanja Boric
From Hans Passant'scomment on my original question:
来自Hans Passant对我最初问题的评论:
Just add 7 to the end day if it is less than the start day. Similarly, add 7 to day if it is less than the start day.
如果它小于开始日,只需在结束日加 7。同样,如果天数小于开始日期,则将天数加 7。
DayOfWeek day = DateTime.Now.DayOfWeek;
DayOfWeek start_day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), sr.start_day);
DayOfWeek end_day = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), sr.end_day);
if (end_day < start_day)
end_day += 7;
if (day < start_day)
day += 7;
if (day >= start_day && day <= end_day)
{
//Action
}
回答by Hogan
if ((day >= DayOfWeek.Monday) && (day <= DayOfWeek.Friday))
{
// action
}
回答by mattiaerre
You can use the DayOfWeek enumeration in order to see if a date is Sunday or Saturday. http://msdn.microsoft.com/en-us/library/system.dayofweek.aspxI hope this can help.
您可以使用 DayOfWeek 枚举来查看日期是星期日还是星期六。http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx我希望这能有所帮助。
回答by HatSoft
The line below will return "Sunday"
下面的行将返回“星期日”
string nameOfTheDay = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo("en-GB")).ToLower();
if(nameOfTheDay != "sunday" && nameOfTheDay != "saturday")
{
//Do Stuff
}
回答by zamir
public bool IsWeekend(DateTime dateToCheck)
{
DayOfWeek day = (DayOfWeek) dateToCheck.Day;
return ((day == DayOfWeek.Saturday) || (day == DayOfWeek.Sunday));
}
回答by Mustafa Gülmez
extention for DateTime
日期时间的扩展
public static bool IsWeekend(this DateTime date)
{
return new[] {DayOfWeek.Sunday, DayOfWeek.Saturday}.Contains(date.DayOfWeek);
}
回答by Michael
This is an elegant solution for the problem. It's a class that can easily be imported into other projects. The coding allows the programmer to dynamically assign what days to check for and pass them as a string array to the class. The data can come from a database or be hard coded when you pass it to an instance of this class for processing. It returns the values of True if you're off work and False if you're working that day. Below the class I provided a simple example of implementation. This class features: Dynamic allocation of what days you have off, Simple error handler by setting strings to lowercase before comparing them, Easily integrated with a database that has your work schedule where your days off may not always be the same. Easily integrated as a hard coded number of days off.
这是该问题的优雅解决方案。这是一个可以轻松导入其他项目的类。编码允许程序员动态分配要检查的日期并将它们作为字符串数组传递给类。数据可以来自数据库,也可以在将其传递给此类的实例进行处理时进行硬编码。如果您下班,则返回 True 值,如果当天正在工作,则返回 False 值。在课程下方,我提供了一个简单的实现示例。这门课程的特点:动态分配休假天数 通过在比较字符串之前将字符串设置为小写的简单错误处理程序 轻松与包含您的工作时间表的数据库集成,而您的休假天数可能并不总是相同。轻松集成为硬编码的休息天数。
// The Class To Check If You're Off Work
class DayOffChecker
{
public bool CheckDays(List<string> DaysOff)
{
string CurrentDay = DateTime.Now.DayOfWeek.ToString();
CurrentDay.ToLower();
foreach (string DayCheck in DaysOff)
{
DayCheck.ToLower();
if (CurrentDay == DayCheck)
{
return (true);
}
}
return (false);
}
}
// Example usage code:
class Program
{
List<string> DaysOff = List<string>();
DaysOff.Add("Saturday"); // Add some values to our list.
DaysOff.Add("Sunday");
DayOffChecker CheckToday = new DayOffChecker();
if(CheckToday.CheckDays(DaysOff))
{
Console.WriteLine("You're Off Today!!!");
}
}
回答by Bala Sravan Dindukurthi
We can also follow similar approach of checking if a given hour is between two hours. Following is the algorithm
我们也可以遵循类似的方法来检查给定的小时是否在两个小时之间。以下是算法
checkIfFallsInRange(index,start,end)
bool normalPattern = start <= end ;
if ( normalPattern)
return index>=start && index<=end;
else
return index>=start || index <=end;
回答by Chris B.
My simple solution to determining if the current day is a workday or not is:
我确定当天是否为工作日的简单解决方案是:
public static bool IsWorkDay(this DateTime dt)
{
return IsWorkDay(dt, DayOfWeek.Sunday, DayOfWeek.Saturday);
}
public static bool IsWorkDay(this DateTime dt, params DayOfWeek[] noneWorkDays)
{
return !noneWorkDays.Contains(dt.DayOfWeek);
}
It assumes Sunday / Saturday are non-work days. Otherwise the user can specify the non-work days. And is an extension for easy of use.
它假定周日/周六是非工作日。否则,用户可以指定非工作日。并且是易于使用的扩展。
Note: To avoid a loop could created a bit flag.
注意:为了避免循环,可以创建一个位标志。
回答by Abdullah AlSubaiee
DayOfWeek Day = DateTime.Today.DayOfWeek;
int Time = DateTime.Now.Hour;
if (Day != DayOfWeek.Saturday && Day != DayOfWeek.Sunday)
{
if (Time >= 8 && Time <= 16)
{
//It is Weekdays work hours from 8 AM to 4 PM
{
}
else
{
// It is Weekend
}

