Get First Date of each month in current year C#



This code will help to get the first date of each month in the current year.

public static List<DateTime> GetFirstDateOfEachMonthInCurrentYear()
{
    List<DateTime> dates = new List<DateTime>();
    for (var i = 1; i <= 12; i++)
    {
        var date = new DateTime(DateTime.Now.Year, i, 1, 0, 0, 0);
        dates.Add(date);
    }
    return dates;
}

//Calling
List<DateTime> list = GetFirstDateOfEachMonthInCurrentYear();
foreach (var date in list){
      Console.WriteLine (date.Date);
}

Output:

01/01/2022 00:00:00
02/01/2022 00:00:00
03/01/2022 00:00:00
04/01/2022 00:00:00
05/01/2022 00:00:00
06/01/2022 00:00:00
07/01/2022 00:00:00
08/01/2022 00:00:00
09/01/2022 00:00:00
10/01/2022 00:00:00
11/01/2022 00:00:00
12/01/2022 00:00:00

0 Comment's

Comment Form

Submit Comment