Asp.net DateTime get code for Monday and Sunday dates

Source: Internet
Author: User

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.

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.