SoFunction
Updated on 2025-03-01

C# Method to get the start date and end date of each year, month, and week

This article describes the method of obtaining the start date and end date of each year, month, and week in C#. Share it for your reference, as follows:

When writing programs, we often have to calculate the start date and end date of year, month, and week, and here we give a unified solution method.

/// <summary>
/// Declare the type enumeration during the period/// </summary>
public enum Period {Day, Week, Month, Year};
/// <summary>
/// Get the start and end date of the specified period/// </summary>
/// <param name="period">period type</param>/// <param name="beginDate">Start Date</param>/// <param name="endDate">End Date</param>public static void GetPeriod(Period period, out DateTime beginDate, out DateTime endDate)
{
  int year = ;
  int month = ;
  switch (period)
  {
    case : //Year      beginDate = new DateTime(year, 1, 1);
      endDate = new DateTime(year, 12, 31);
      break;
    case : //moon      beginDate = new DateTime(year, month, 1);
      endDate = (1).AddDays(-1);
      break;
    case : //week      int week = (int);
      if (week == 0) week = 7; //Sunday      beginDate = (-(week - 1));
      endDate = (6);
      break;
    default: //day      beginDate = ;
      endDate = ;
      break;
  }
}

PS: Here are a few date and time-related tools for your reference:

In the date day difference calculator:
http://tools./jisuanqi/onlinedatejsq

Online date calculator/phase difference day calculator:
http://tools./jisuanqi/datecalc

Online Date/Day Calculator:
http://tools./jisuanqi/date_jisuanqi

Online Lunar/Gregorian calendar conversion tool:
http://tools./bianmin/yinli2yangli

Online Day Calculator (flash version):
http://tools./jisuanqi/datejsq

For more information about C#, please visit the special topic of this site:Summary of C# date and time operation skills》、《Summary of C# string operation skills》、《Summary of C# array operation skills》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial"and"Introduction to C# object-oriented programming tutorial

I hope this article will be helpful to everyone's C# programming.