DateTime dt = DateTime.Now; Current time
DateTime Startweek = Dt.adddays (1-convert.toint32 (dt.dayofweek.tostring ("D"))); This week Monday
DateTime Endweek = startweek.adddays (6); This Sunday
DateTime startmonth = Dt.adddays (1-dt.day); Earlier this month
DateTime endmonth = startmonth.addmonths (1). AddDays (-1); Month End
DateTime endmonth = Startmonth.adddays ((dt.addmonths (1)-DT). Days-1); Month End
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); At the end of the quarter
DateTime startyear = new DateTime (Dt.year, 1, 1); Early this year
DateTime endyear = new DateTime (Dt.year, 12, 31); At the end of this year
#region get the Monday and Sunday dates of the week
<summary>
Calculate the week of 1st of the week
</summary>
<returns></returns>
public static datetime getmondaydate ()
{
Return Getmondaydate (DateTime.Now);
}
<summary>
Calculate the date of the week Sunday
</summary>
<returns></returns>
public static datetime getsundaydate ()
{
Return Getsundaydate (DateTime.Now);
}
<summary>
Calculates the starting date of a day (on Monday)
</summary>
<param name= "Somedate" > Any day of the week </param>
<returns> returns the date of worship, followed by the exact time, minutes, seconds, and incoming values equal </returns>
public static datetime getmondaydate (DateTime somedate)
{
int i = Somedate.dayofweek-dayofweek.monday;
if (i = = 1) i = 6;//i > = 0, because of the enumeration reason, Sunday is ranked first, at this time sunday-monday=-1, must +7=6.
TimeSpan ts = new TimeSpan (i, 0, 0, 0);
return somedate.subtract (TS);
}
<summary>
Calculate a day end date (day of worship)
</summary>
<param name= "Somedate" > Any day of the week </param>
<returns> return to day of worship, followed by time, minutes, seconds and incoming values equal </returns>
public static datetime getsundaydate (DateTime somedate)
{
int i = Somedate.dayofweek-dayofweek.sunday;
if (i!= 0) i = 7-i;//because of the enumeration reason, the Sunday is ranked first, and the subtraction interval is reduced by 7.
TimeSpan ts = new TimeSpan (i, 0, 0, 0);
return Somedate.add (TS);
}
#endregion
Date converted to character type
String t = row["Time"].tostring ()
String time = DateTime.Parse (t). ToString ("Yyyy-mm-dd");
Convert.todatetime ("07-05-2007"). ToString ("YYYY-MM-DD") this code is converted to a string type