C # determine whether the current time is working time

Source: Internet
Author: User

 

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 " ;
}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.