Some time ago, when I was working on a project, I thought it was interesting to determine whether the current time was working or not. So I would like to share it with you and learn it together.
Note:
1. _ strworkingdayam equivalent. The default value is assigned here. The configuration file should be read normally.
2. The clsarraylist class is a function class that intercepts strings into arrays. It has been written in my blog. If you are interested, you can take a look.
Private String _ Strworkingdayam = " 08:00 " ; // Working hours: AM
Private String _ Strworkingdaypm = " 16: 00 " ;
Private String _ Strrestday = " 6, 7 " ; // The day of the week, the day of the week, and the day of Sunday are 6, 7.
Private Timespan dspworkingdayam; // Working hours: AM
Private Timespan dspworkingdaypm;
Clsarraylist myarraylist;
Private Bool M_isworkingday ()
{
String Strweeknow = This . M_getweeknow (); // Current week
// Determine whether there is a day off
For ( Int I = 0 ; I < Myarraylist. p_count; I ++ )
{
If (Strweeknow = Myarraylist. m_getitem (I ))
{
Return False ;
}
}
// Determine whether the current time is within the working period
Timespan dspnow = Datetime. Now. timeofday;
If (Dspnow > Dspworkingdayam && Dspnow < Dspworkingdaypm)
{
Return True ;
}
Return False ;
}
// Initialize the default value
Private Void M_initworkingday ()
{
Dspworkingdayam = Datetime. parse (_ strworkingdayam). timeofday;
Dspworkingdaypm = Datetime. parse (_ strworkingdaypm). timeofday;
Myarraylist = New Myclass. clsarraylist (_ strrestday, ' , ' );
}
// Call
Void Button1click ( Object Sender, eventargs E)
{
This . M_initworkingday ();
Bool BOL = This . M_isworkingday ();
MessageBox. Show (BOL. tostring ());
} // Obtain the day of the current week
Private String M_getweeknow ()
{
String Strweek = Datetime. Now. dayofweek. tostring ();
Switch (Strweek)
{
Case " Monday " :
Return " 1 " ;
Case " Tuesday " :
Return " 2 " ;
Case " Wednesday " :
Return " 3 " ;
Case " Thursday " :
Return " 4 " ;
Case " Friday " :
Return " 5 " ;
Case " Saturday " :
Return " 6 " ;
Case " Sunday " :
Return " 7 " ;
}
Return " 0 " ;
}