"Reprint" Some extensions to C#datetime, calculate the first day of the week, the last day

Source: Internet
Author: User


/// <summary>
    /// Some extensions of DateTime
    /// </ summary>
    public class DateTime2
    {
        /// <summary>
        /// get the first day of the week (take Sunday as the first day)
        /// </ summary>
        /// <param name = "datetime"> </ param>
        /// <returns> </ returns>
        public DateTime GetWeekFirstDaySun (DateTime datetime)
        {
            // Sunday is the first day
            int weeknow = Convert.ToInt32 (datetime.DayOfWeek);
            int daydiff = (-1) * weeknow;

            // First day of the week
            string FirstDay = datetime.AddDays (daydiff) .ToString ("yyyy-MM-dd");
            return Convert.ToDateTime (FirstDay);
        }

        /// <summary>
        /// Get the first day of the week (take Monday as the first day)
        /// </ summary>
        /// <param name = "datetime"> </ param>
        /// <returns> </ returns>
        public DateTime GetWeekFirstDayMon (DateTime datetime)
        {
            // Monday is the first day
            int weeknow = Convert.ToInt32 (datetime.DayOfWeek);

            // Because Monday is the first day, if you want to judge weeknow equal to 0, you need to push forward 6 days.
            weeknow = (weeknow == 0? (7-1): (weeknow-1));
            int daydiff = (-1) * weeknow;

            // First day of the week
            string FirstDay = datetime.AddDays (daydiff) .ToString ("yyyy-MM-dd");
            return Convert.ToDateTime (FirstDay);
        }

        /// <summary>
        /// get the last day of the week (take Saturday as the last day)
        /// </ summary>
        /// <param name = "datetime"> </ param>
        /// <returns> </ returns>
        public DateTime GetWeekLastDaySat (DateTime datetime)
        {
            // Saturday is the last day
            int weeknow = Convert.ToInt32 (datetime.DayOfWeek);
            int daydiff = (7-weeknow)-1;

            // last day of the week
            string LastDay = datetime.AddDays (daydiff) .ToString ("yyyy-MM-dd");
            return Convert.ToDateTime (LastDay);
        }

        /// <summary>
        /// get the last day of the week (take Sunday as the last day)
        /// </ summary>
        /// <param name = "datetime"> </ param>
        /// <returns> </ returns>
        public DateTime GetWeekLastDaySun (DateTime datetime)
        {
            // Sunday is the last day
            int weeknow = Convert.ToInt32 (datetime.DayOfWeek);
            weeknow = (weeknow == 0? 7: weeknow);
            int daydiff = (7-weeknow);

            // last day of the week
            string LastDay = datetime.AddDays (daydiff) .ToString ("yyyy-MM-dd");
            return Convert.ToDateTime (LastDay);
        }
    } 


Original link: http://blog.csdn.net/joyhen/article/details/9179763



"Reprint" Some extensions to C#datetime, calculate the first day of the week, the last day


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.