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