asp.net datetime Monday and Sunday date get code

Source: Internet
Author: User
Tags current time

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

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.