Asp.net time class Monday and weekend dates of a week

Source: Internet
Author: User
Tags net time

In many cases, this week's statistics are required for database query statistics.
I don't want to hand over the computing task to the data here.
So I wrote a small function myself to get the time of Monday and weekend of this week. When the database is compared, I only need between... and.
Code As follows:
# 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
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.