### Simple Age Calculation

I was helping someone with some SQL stuff and needed to give them a function to calculate someone's age.  Rooted around a bunch of places and finally just ended up re-writing it. So this is mostly to have it on hand. This function is needed because DateDiff doesn't check if they've had their birthday this year.

```CREATE FUNCTION CalculateAge (
@Start smalldatetime
, @End smalldatetime
)
RETURNS int
AS
BEGIN
-- Declare the return variable here
DECLARE @Age int

SET @Age = DateDiff(year, @Start, @End)
IF DateAdd(year, @Age, @Start) > @End
SET @Age = @Age - 1

RETURN @Age

END
GO```

VB Version

```Public Function Age(ByVal value As Date, ByVal d As Date) As Integer
Age = DateDiff(DateInterval.Year, value, d)
If value.AddYears(Age) > d Then Age = Age - 1
Return Age
End Function``` shital said…

Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well.

PMP Certification

### JavaScript function to automatically add slashes to date

In converting an old Windows app to a browser app, the user wanted to be able to enter dates without the slashes. Here's a simple jscript: 1: // Function to convert short date string (MMddyy) 2: // or (MMddyyyy) to a date string (mm/dd/yyyy). 3: // txtBox is the actual textbox control 4: // with the value to be processed. 5: function FixShortDate(txtBox) { 6: if (txtBox == null ) { 7: return '' } 8:   9: var re = new RegExp(/(\d{6})(\d{2})?/); 10:   11: if (re.test(txtBox.value)) 12: { 13: if (txtBox.value.length == 8) { 14: txtBox.value = txtBox.value.substring(0, 2) + '/' + txtBox.value.substring(2, 4) + '/' + txtBox.value.substring(4, 8) 15: } 16: 17: if (txtBox.value.length == 6) { 18: if (txtB