Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
Good day

I am a beginner to vb , I am trying to get the last day of the month using the datetimepiker. When i use the up-down control i receive an error ( 'Year, Month, and Day parameters describe an un-representable DateTime.) when month days are not match (31 or 30). I have used the below code.


What I have tried:

dtp2.Value = DateSerial(dtp2.Value.Year, dtp2.Value.Month,1).AddMonths(1).AddDays(-1)
Updated 6-Sep-19 23:39pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Don't use outdated VB commands like DateSerial: use the DateTime constructor instead:
dtp2.Value = New DateTime(dtp2.Value.Year, dtp2.Value.Month, 1).AddMonths(1).AddDays(-1)
qulaitks 7-Sep-19 5:50am
Thanks OriginalGriff for the quick respond. I have tried the given code but still reciving the same error massage.

OriginalGriff 7-Sep-19 6:04am
So use the debugger and find out exactly aht dtp2 holds ... the code works here, so it has to be data related, and we don't have access to that!
zamanipour 7-Sep-19 8:10am
maybe the problem raise from the initial value of dtp2.Value. check it to be valid or use a valid date for test, like : 'm = new DateTime()' and then: 'dtp2.Value = New DateTime(m.Year, m.Month, 1).AddMonths(1).AddDays(-1)'
qulaitks 7-Sep-19 21:46pm
Good Morning

I am using the DTP that has format (MMM-yyyy) to filter DGV , the code above works fine if months days are the same , as soon as the months days changes from say 31 to 30 , I receive the erroe massage. Sorry if i did not explain things right.

best regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100