Start New Discussion within our Software Development Community

Good day!

I just need a little help on how to get the last months date (dd/mm/yyyy) between two dates? I have two date pickers(fromdate & todate). Ex. If fromdate is 24/01/2015 and todate is 24/12/2015, it should enumerate the end of the months starting fromdate to todate. So the result is like below:

31/01/2015
28/02/2015
31/03/2015
...
...
...
31/12/2015

I currently have the ff. code, but it does not get the end of the day of the month, instead it shows only the next date of the month the same as the previous date.

Dim lastmonthdates As Date

lastmonthdates = dtfrom.Value
While DateSerial(Year(lastmonthdates), Month(lastmonthdates), 0) < DateSerial(Year(dtto.Value), Month(dtto.Value), 0)
    lastmonthdates = DateSerial(Year(lastmonthdates), Month(lastmonthdates) + 1, Day(lastmonthdates))
    MsgBox lastmonthdates
Wend

Thank you for helping..!

I think I got it.. But any better solutions would be better..

Dim lastmonthdates As Date
Dim runningdate As Date

runningdate = DateSerial(Year(dtfrom.Value), Month(dtfrom.Value) - 1, Day(dtfrom.Value))
While runningdate < DateSerial(Year(dtto.Value), Month(dtto.Value), 0)
    runningdate = DateSerial(Year(runningdate), Month(runningdate) + 1, Day(runningdate))
    lastmonthdates = DateSerial(Year(runningdate), Month(runningdate) + 1, 0)
    MsgBox lastmonthdates
Wend

Thank you!

This question has already been answered. Start a new discussion instead.