C # Gets the first and last day of the week on which a date is located

Source: Internet
Author: User


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WyfClass
{
public class tools
{
/// <summary>
/// get the first day of the week
/// </ 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 determine that weeknow is equal to 0, push it forward for 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);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WyfClass
{
public class tools
{
/// <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 determine that weeknow is equal to 0, push it forward for 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);
}
}
} 





C # Gets the first and last day of the week on which a date is located


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.