# Visual Basic Date Math in LightSwitch

• ### Question

• I want need to do a bunch of date math.  As a basic one, I need to calculate a persons age based on their date of birth.  I have done this in Word macros using VBA DateDiff and DateSerial functions. But even though I'm in the VB version of LS (not the C# one), it seems LS doesn't recognize these functions.

What am I missing?  Can someone help show me how to do date math in LS?

Thanks,

Tuesday, February 22, 2011 3:16 AM

• I can see where you're coming from, since not all years will have 365 days due to leap years.

I guessing that you're trying to do this calucation from a computed field. If you add the following imports to the top of your code file, the DateDiff function should hopefully be available.

```Imports Microsoft.VisualBasic

```

• Marked as answer by Wednesday, February 23, 2011 4:11 AM
Tuesday, February 22, 2011 1:48 PM

### All replies

• This is not really specific to LightSwitch, but the .NET Framework supports mathematical operations on dates without the use of helping functions such as DateDiff from VBA.

For example, you can subtract two Date objects and get a TimeSpan as the result. You can then divide the Days property of the TimeSpan by the number of days in the year to calculate the difference in years between the two dates.

```Private Function GetAge(ByVal birthdate As Date) As Integer
Dim difference As TimeSpan = Date.Now - birthdate
Return difference.Days / 365
End Function
```

Tuesday, February 22, 2011 9:28 AM
• Thanks for the tip, but it is less precise that the other way.  This is how I've been doing it:

vYears = DateDiff("yyyy", vStartDate, Now)

If Date < DateSerial(Year(Now), Month(vStartDate), Day(vStartDate)) Then

vYears = vYears - 1

End If

How can I do that in LS?  I'm running the Visual Basic "template", so I don't understand why DateDiff and DateSerial are giving me errors.

Tuesday, February 22, 2011 12:39 PM
• Thanks for the tip, but it is less precise that the other way.  This is how I've been doing it:

vYears = DateDiff("yyyy", vStartDate, Now)

If Date < DateSerial(Year(Now), Month(vStartDate), Day(vStartDate)) Then

vYears = vYears - 1

End If

How can I do that in LS?  I'm running the Visual Basic "template", so I don't understand why DateDiff and DateSerial are giving me errors.

first you need to understand that you are now using a different version of basic.

VB.Net is not a version of VBA

not all of the VBA code will work in VB.Net

first thing is for the IF you want to do is i would look at the DateTime constuctor - you can create a datetime like this

DateTime( yyyy,mmmm,dddd )

and use that date time for your compare test.

also look at the TimeSpan class  .... I think it will have a property to return years as well as days.

Tuesday, February 22, 2011 1:32 PM
• I can see where you're coming from, since not all years will have 365 days due to leap years.

I guessing that you're trying to do this calucation from a computed field. If you add the following imports to the top of your code file, the DateDiff function should hopefully be available.

```Imports Microsoft.VisualBasic

```

• Marked as answer by Wednesday, February 23, 2011 4:11 AM
Tuesday, February 22, 2011 1:48 PM
• `"Imports Microsoft.VisualBasic" was the PERFECT solution as I can now use a lot of my old code. I viewed the code for that table (yes, it is a computed field) and I put it in above the Namespace for my app. As such, can I do that just once or does it have to be added to the top of every table's code?`
Wednesday, February 23, 2011 4:13 AM
• Glad that works for you!

To not have to add this line of code to all code files, switch your project into 'file view'. In 'Solution Explorer', theres a button that allows you to switch between 'logical view' and 'file view'.

Once in 'file view', you'll see a project called 'Client'. Right click on this, open up the properties and go to the references tab. A grid appears with 'Imported namespaces'. Click on the 'Microsoft.VisualBasic' tickbox.
Wednesday, February 23, 2011 9:51 AM
• That was already checked (so, did it get checked because I added the Imports line ?).

If it was already checked, then why did I need to add the Imports line?

BTW, to try to make my own custom add screen, another thread said I needed:

Imports System.Windows.Controls

But this didn't work.  I "checked" this in same Imports screen, but it wouldn't let me Update User Import (it stays grayed out).

How can I do that?

Thanks

Thursday, February 24, 2011 3:50 AM