Get valid working days between any two dates (excluding Saturday) get valid working days between any two dates (excluding Saturday) # region obtains valid working days between any two dates (excluding Saturday)
// Call
// Datetime date1 = convert. todatetime ("2005-10-20 ");
// Datetime date2 = convert. todatetime ("2005-11-01 ");
// Int days = getdays (date1, date2 );
Private int getdays (system. datetime date1, system. datetime date2)
{
String M = datediff (enumdatecompare. Day, date1, date2). tostring ("F0 ");
Int Mm = 0;
For (INT I = 0; I <= convert. toint32 (m); I ++)
{
System. datetime date = convert. todatetime (date1.adddays (I ));
Switch (date. dayofweek)
{
Case System. dayofweek. Monday:
Case System. dayofweek. Thursday:
Case System. dayofweek. Tuesday:
Case System. dayofweek. Wednesday:
Case System. dayofweek. Friday:
Mm = mm + 1;
Break;
}
}
Return mm;
}
# Endregion
Format output
Format output # region format output
Private void page_load (Object sender, system. eventargs E)
{
System. Globalization. datetimeformatinfo mydtfi = new system. Globalization. cultureinfo ("En-us", false). datetimeformat;
// ZH-CN for China
Datetime mydt = system. datetime. now;
Response. Write (mydt. tostring ("F", mydtfi ));
/**//**//**//*
This code produces the following output.
Format en-US example
Char value of associated property, if any
D 1/3/2002 M/D/YYYY (shortdatepattern)
D Thursday, January 03,200 2 dddd, Mmmm DD, YYYY (longdatepattern)
F Thursday, January 03,200 2 AM
F Thursday, January 03,200 2 12:00:00 am dddd, Mmmm DD, yyyy H: mm: ss tt (fulldatetimepattern)
G 1/3/2002 12: 00 AM
G 1/3/2002 12:00:00 AM
M January 03 mmmm dd (monthdaypattern)
M January 03 mmmm dd (monthdaypattern)
R Thu, 03 Jan 2002 00:00:00 gmt ddd, DD Mmm yyyy HH ': 'mm': 'ss 'gmt' (rfc1123pattern)
R Thu, 03 Jan 2002 00:00:00 gmt ddd, DD Mmm yyyy HH ': 'mm': 'ss 'gmt' (rfc1123pattern)
S 2002-01-03t00: 00: 00 yyyy '-'mm'-'dd't'hh': 'mm': 'ss (sortabledatetimepattern)
T 12:00 am h: Mm TT (effectimepattern)
T 12:00:00 Am h: mm: ss tt (longtimepattern)
U 2002-01-03 00: 00: 00Z yyyy '-'mm'-'dd hh': 'mm': 'ss 'Z' (universalsortabledatetimepattern)
U Thursday, January 03,200 2 8:00:00 AM
Y January, 2002 Mmmm, YYYY (yearmonthpattern)
Y January, 2002 Mmmm, YYYY (yearmonthpattern)
*/
}
# Endregion
Get Saturday and Sunday of the week
Convertdatetoweek # region convertdatetoweek
Public static void convertdatetoweek (datetime date, out datetime firstdate, out datetime lastdate)
{
Datetime first = system. datetime. now;
Datetime last = system. datetime. now;
Switch (date. dayofweek)
{Case System. dayofweek. Monday:
First = date. adddays (-1 );
Last = date. adddays (5 );
Break;
Case System. dayofweek. Tuesday:
First = date. adddays (-2 );
Last = date. adddays (4 );
Break;
Case System. dayofweek. Wednesday:
First = date. adddays (-3 );
Last = date. adddays (3 );
Break;
Case System. dayofweek. Thursday:
First = date. adddays (-4 );
Last = date. adddays (2 );
Break;
Case System. dayofweek. Friday:
First = date. adddays (-5 );
Last = date. adddays (1 );
Break;
Case System. dayofweek. Saturday:
First = date. adddays (-6 );
Last = date;
Break;
Case System. dayofweek. Sunday:
First = date;
Last = date. adddays (6 );
Break;
}
Firstdate = first;
Lastdate = last;
}
# Endregion
// Call
Datetime firstdate = system. datetime. now;
Datetime lastdate = system. datetime. now;
Convertdatetoweek (date, out firstdate, out lastdate );
The week of the year when the current date is obtained.
Datetime dt = convert. todatetime ("2006-05-01 ");
Int weeks = DT. dayofyear/7 + 1;