Calculating number of days between dates
-
2012年8月8日 3:23The 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.
http://msdn.microsoft.com/en-us/library/se0w9esz(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/cb8x3kfz.aspx
http://msdn.microsoft.com/en-us/library/ea1sssb2.aspx- 回答の候補に設定 Mark Liu-lxfModerator 2012年8月9日 3:05
- 回答としてマーク Mark Liu-lxfModerator 2012年8月16日 7:25
-
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
- 回答の候補に設定 Mark Liu-lxfModerator 2012年8月9日 3:04
- 回答としてマーク Mark Liu-lxfModerator 2012年8月16日 7:26
-
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 Label1.Text = deltaDays End Sub End ClassIch 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 Label1.Text = deltaDays 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.
- 編集済み John Anthony Oliver 2012年8月8日 14:20
- 編集済み John Anthony Oliver 2012年8月9日 15:12
-
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
The mod operator requires two operands. These can be any expression of a supported type, including a variable.Is it possible to use a variable with Mod? I need this program to be able to figure just about any span of years.
Armin
-
2012年8月8日 20:04
Hi John,
oh yes, OK :-)
Ellen
Ich benutze/ I'm using VB2008 & VB2010

