How to Get the Last date of every Month From a specific date till Current Date

Hi All,
I want to get all the dates of every month from a specific date till today/Current date. Like i have a date 11-Sep-23 so i need the all last dates of every month till today 15-Nov-23. The result should be, 31-Sep-23 last date, 30-Oct-23 last date oct, 31-Nov-23 last day of oct.

Hi @tayyabimtiaz1,

Dim startDate As DateTime = DateTime.ParseExact("11-Sep-23", "dd-MMM-yy", CultureInfo.InvariantCulture)
Dim currentDate As DateTime = DateTime.Now.Date
Dim lastDates = Enumerable.Range(0, 1 + (currentDate - startDate).Days \ 30).
    Select(Function(i) New DateTime(startDate.AddMonths(i).Year, startDate.AddMonths(i).Month, DateTime.DaysInMonth(startDate.AddMonths(i).Year, startDate.AddMonths(i).Month))).
    Where(Function(d) d <= currentDate).
    ToList()

For Each lastDate In lastDates
    Console.WriteLine("Last date of month: " & lastDate.ToString("dd-MMM-yy"))
Next

@tayyabimtiaz1

try this

  1. While loop with condition as Not DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).Year.Equals(Now.Year) AndAlso DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).Month.Equals(Now.Month)
  2. Inside loop use log message with DateTime.DaysInMonth(DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).Year,DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).Month).ToString + DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).ToString("-MMM-yy") + "Last Date of " + DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).Month
  3. Next activity would be assign with str_GivenDate = DateTime.ParseExact(str_GivenDate,"dd-MMM-yy",System.Globalization.CultureInfo.InvariantCulture).AddDays(1).ToString("dd-MMM-yy")

cheers

We would have many options and will give starter help done with essential modellings:

Variables:

Modelling

tmpMonthEnd = new DateTime(startDate.Year, startDate.Month, 1).AddMonths(idx + 1).AddDays(-1)
MonthEndDates = MonthEndDates.Append(tmpMonthEnd).tolist

grafik

1 Like

Getting error for list(String)

When doing it with LINQ (a medium long oneliner):

We keep in mind that different month do have different days count: e.g. Feb 28, Dez 31) so we avoid fixed month length integer divisions

Enumerable.Range(0,(endDate - startDate).Days).Select(Function (x) CDate(startDate.AddDays(x).ToString("MM/01/yyyy")).AddMonths(1).AddDays(-1)).Distinct().toList

Hi @tayyabimtiaz1

Can you try this

startDate = New DateTime(2023, 9, 11) // Set your specific start date
currentDate = DateTime.Now
endOfMonthDates = new List(Of DateTime)

While
startDate <= currentDate
// Find the last day of the current month
lastDayOfMonth = New DateTime(startDate.Year, startDate.Month, DateTime.DaysInMonth(startDate.Year, startDate.Month))
If lastDayOfMonth <= currentDate Then
endOfMonthDates.Add(lastDayOfMonth)
End If

startDate = startDate.AddMonths(1)

End While

Use Excel Application Scope to write the endOfMonthDates to an Excel file

Hope this helps

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.