Lightswitch difference between two dates in months (VB) does not have to be exact

I have two date entries that are filled out with random dates (datfields). I want to compute the time between two dates in months. Dividing the days by 30 is fine does not have to be exact. The computed field is a String. Not sure if this matters or if it would be better as an integer.

I know the following is not correct, but it should give some type of idea of what I am trying to do.

            If Not StartDate Is Nothing And Not EndDate Is Nothing Then
                result = (StartDate - EndDate) /30
Who is Participating?
Jacques Bourgeois (James Burger)PresidentCommented:
The method works on Date variables. Your dates are probably in another format such as a String.

Try the following:

Datediff(DateInterval.Month, CDate(StartDate), CDate(EndDate))
Jacques Bourgeois (James Burger)PresidentCommented:
Assuming dates in d1 and d2:

DateDiff(DateInterval.Month, d2, d1)
Rlm214Author Commented:
I've tried the above. However it seems that actual dates need listed in that code to work. I have two dates for each entry tracking in my program. I need it to look at those two dates and come up with the difference in months.

I receive the following error with the code below.

Datediff(DateInterval.Month, StartDate, EndDate)

I get the error "Overload resolution failed because no acessible 'DateDiff' can be called wthout a narrowing conversion:........
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Rlm214Author Commented:
To be more specific about the situation

Name: LeaseCommencementDate
Type: DATE

Name LeaseExpirationDate
Type: DATE

Properties: IS COMPUTED

Error =
Jacques Bourgeois (James Burger)PresidentCommented:
If I understand, CDate corrected the call to DiffDate, but you get a new error this time.

This one happens somewhere else and might be more difficult to grasp because it does not come from your code.

Why do you store the result in a String, while it is a number? Always try to use the correct type for values. Dates should be in Date variables, numbers in the most appropriate type for the values you expect, most of the time an Integer for numbers that do not have decimals. Otherwise you often end up with conversion problems here and there.

But that might not be your new problem. Where does that MULTI_USER comes from?
Rlm214Author Commented:
Things seem to be on the right track. Even with the error during my upload, your code is working in my application. The months are being calculated as expected.

In my SQL database lists my date entries as DateTime. My lightswitch application lists them as Dates. I'm not sure if this has anything to do with it or not. Possibly that my computed field is a string instead of integer?

The important thing is that the code is working. I can work on the discrepancy between lightswitch and the database.
Rlm214Author Commented:
I resolved the error. Thanks again for your help!!!

The error was coming from publishing my lightswitch program with the "TotalTerm" as a non calculated field, then trying to publish again after making it a calculated field.

To correct the error, I deleted "TotalTerm" from my database and everything is now working as planned.
Jacques Bourgeois (James Burger)PresidentCommented:
You do not have to "fear" differences in types between SQL Server and VB.

The Date type in VB is in fact a datetime, it always contain time information along with the date. Anyway, it does not hold the information in the same format as SQL Server, but the conversion is automatically done for you by the methods that LightSwitch use to communicate with SQL Server.

The same holds true for many types of variables. For instance, VB as only String to work with text, while SQL Server has many different types for Text. They will always be mapped correctly for you in the background.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.