Calculating number of days between dates

# Calculating number of days between dates

• 2012年8月8日 3:23

The dates may be in different years.  We are not allowed to use VB built-in functions like datediff and we have to account for Leap years.  I have everything else figured out except accounting for the Leap years.  I know they are every four years and if the year ends in 00 it has be evenly divisible by 400.  I just don't know how to code those selections.

Ken Davy Dayton, OH

### すべての返信

• 2012年8月8日 4:00

The  Mod operator is relevant for your problem.   If you take a number 'Mod 4' then you get 0 if it is evenly divisible by 4.  If you take a number 'Mod 400', then you get a zero if it is evenly divisible by 400, and so on.

A year will be a leap year if it is evenly divisible by 4 but not evenly divisible by 100, or if it is evenly divisible by 400.

Your task is to construct a compound IF statement that will express that rule correctly.

• 2012年8月8日 10:42

Maybe I missed something, but...:

```      Dim d1, d2 As Date
Dim diff = d2 - d1

MessageBox.Show(diff.TotalDays.ToString)
```
No VB built-in functions used, just core framework methods.

Armin

• 2012年8月8日 13:17

Hello,

try this sample (i hope it will work also in your culture)

regards Ellen

```Public Class Form1
Private date1, date2 As DateTime
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Not DateTime.TryParse(TextBox1.Text, date1) Then
MsgBox("invalid: " & TextBox1.Text)
End If
If Not DateTime.TryParse(TextBox2.Text, date2) Then
MsgBox("invalid: " & TextBox2.Text)
End If
Dim delta As TimeSpan = date1 - date2
Dim deltaDays As String = delta.Days.ToString

End Sub
End Class```

Ich benutze/ I'm using VB2008 & VB2010

• 2012年8月8日 14:18

Hi Ellen,

Here is a slight change to your code. See what you think.  :)

I have changed the first two If tests for DO UNTIL loops.

That way the code does not proceed beyond the Exit Sub

statements and the code also allows for the user to correct His / Her input.

'

MessageBox.Show

has more options than MsgBox too.

'

```Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim date1, date2 As Date

Do Until DateTime.TryParse(TextBox1.Text, date1) = True
MessageBox.Show("invalid: " & TextBox1.Text)
'If you get this far Exit the Sub until we have valid input.>>
Exit Sub
Loop

Do Until DateTime.TryParse(TextBox2.Text, date2) = True
MessageBox.Show("invalid: " & TextBox2.Text)
'If you get this far Exit the Sub until we have valid input.>>
Exit Sub
Loop

Dim delta As TimeSpan = date1 - date2
Dim deltaDays As String = delta.Days.ToString

End Sub
End Class```

##### Regards,

Click this link to see the NEW way of how to insert a picture into a forum post.

Installing VB6 on Windows 7

App Hub for Windows Phone & XBOX 360 developers.

• 2012年8月8日 18:57

Is it possible to use a variable with Mod?  I need this program to be able to figure just about any span of years.

Ken Davy Dayton, OH

• 2012年8月8日 19:24

Is it possible to use a variable with Mod?  I need this program to be able to figure just about any span of years.

The mod operator requires two operands. These can be any expression of a supported type, including a variable.

Armin

• 2012年8月8日 20:04

Hi John,

oh yes, OK :-)

Ellen

Ich benutze/ I'm using VB2008 & VB2010