Datetime dt = datetime. now; // current time
Datetime startweek = dt. adddays (1-convert. toint32 (dt. dayofweek. tostring ("d"); // Monday of the week
Datetime endweek = startweek. adddays (6); // Sunday of the week
Datetime startmonth = dt. adddays (1-dt. day); // the beginning of this month
Datetime endmonth = startmonth. addmonths (1). adddays (-1); // End of the month
// Datetime endmonth = startmonth. adddays (dt. addmonths (1)-dt). days-1); // End of the month
Datetime startquarter = dt. addmonths (0-(dt. month-1) % 3). adddays (1-dt. day); // at the beginning of this quarter
Datetime endquarter = startquarter. addmonths (3). adddays (-1); // end of this quarter
Datetime startyear = new datetime (dt. year, 1, 1); // early this year
Datetime endyear = new datetime (dt. year, 12, 31); // End of the year
# Region returns the Monday and Sunday dates of the week.
/// <Summary>
/// Calculate the Monday of the week
/// </Summary>
/// <Returns> </returns>
Public static datetime getmondaydate ()
{
Return getmondaydate (datetime. now );
}
/// <Summary>
/// Calculate the date of this week and Sunday
/// </Summary>
/// <Returns> </returns>
Public static datetime getsundaydate ()
{
Return getsundaydate (datetime. now );
}
/// <Summary>
/// Calculate the start date of a day (the date of Monday)
/// </Summary>
/// <Param name = "somedate"> any day of the week </param>
/// <Returns> returns the day of the week. The specific time, minute, and second are equal to the input value. </returns>
Public static datetime getmondaydate (datetime somedate)
{
Int I = somedate. dayofweek-dayofweek. Monday;
If (I =-1) I = 6; // I value> = 0. Due to enumeration, Sunday is at the top of the list. At this time, Sunday-Monday =-1, must be + 7 = 6.
Timespan ts = new timespan (I, 0, 0, 0 );
Return somedate. subtract (ts );
}
/// <Summary>
/// Calculate the end date of a day (the date of Sunday)
/// </Summary>
/// <Param name = "somedate"> any day of the week </param>
/// <Returns> returns the Sunday date, followed by the same time, minute, and second as the input value </returns>
Public static datetime getsundaydate (datetime somedate)
{
Int I = somedate. dayofweek-dayofweek. Sunday;
If (I! = 0) I = 7-I; // due to enumeration, Sunday is at the top of the list, and the subtraction interval is reduced by 7.
Timespan ts = new timespan (I, 0, 0, 0 );
Return somedate. add (ts );
}
# Endregion
Convert a date to a numeric type
String t = row ["time"]. tostring ()
String time = datetime. parse (t). tostring ("yyyy-mm-dd ");
Convert. todatetime ("07-05-2007"). tostring ("yyyy-mm-dd") the converted code is of the string type.