468,290 Members | 2,019 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,290 developers. It's quick & easy.

Day Finder Program

Hi,

I'm very new to VB and need help please. I want to build a 'Day Finder' program that tells you the day for a given date between 1600 & 2100 and whether it is past, present or future. I have coded the variables and algorithm for a form with 3 text box entries and a command button . How do I get VB to recognise whether the date is past, present or future i.e. by looking to today's date? Is there any existing VB code out there? (i've had a look and could only find a c++ program which doesn't help), thanks
Jul 13 '07 #1
8 2369
ilearneditonline
130 Expert 100+
can you provide a code snippet of what you have done thus far?
Jul 13 '07 #2
Mague
137 100+
can you provide a code snippet of what you have done thus far?

Im assuming you are using vb.net cause you said you were new

I dont know excally what you want it to do but hope this helps.

Dim ts As TimeSpan
ts = (CType(DateTimePicker1.Value.Subtract(Today), TimeSpan))
If ts.TotalDays < 1 Then
Label1.Text = "Timespan: Past Date or Today's Date"
Else : Label1.Text = "Timespan: Future Date"
End If

I found this on google. I dont understand it but you might want to

The website is
http://blogs.vbcity.com/xtab/archive/2005/12/26/5755.aspx

Hope this helps
Mague
Jul 13 '07 #3
pureenhanoi
175 100+
Hi,

I'm very new to VB and need help please. I want to build a 'Day Finder' program that tells you the day for a given date between 1600 & 2100 and whether it is past, present or future. I have coded the variables and algorithm for a form with 3 text box entries and a command button . How do I get VB to recognise whether the date is past, present or future i.e. by looking to today's date? Is there any existing VB code out there? (i've had a look and could only find a c++ program which doesn't help), thanks
Subtraction two date directly. If the result < 0 then the date is in the past. If the result >0 then the date is in future.
If ur data was not already the DateTime Type, so, convert it into DateTime first
Example
Date - (Date+1) will return -1
Date - (Date-1) will return +1
(urDate - Date ) < 0 then urDate is in the past
Jul 14 '07 #4
great, thanks all. this is a DateDiff function which i want to use e.g:

Private Function IsPPOrF(ByVal strDate As String) As String
If Not IsDate(strDate) Then
IsPPOrF = "Bad date value"

Else
Select Case DateDiff("d", Now, strDate)
Case 0
IsPPOrF = "Present"
Case Is < 0
IsPPOrF = "Past"
Case Is > 0
IsPPOrF = "Future"
End Select
End If
End Function

how would i add that to my code (which is awful but just about works) so my msgbox says "the date on the 14 jun 1977 is a friday past/present/future"? (it is down at the bottom)

Public Class Gregorian_Calendar_2

'Declare variables

Dim Day As Integer
Dim dayNumber As Integer
Dim Month As String
Dim MonthCase As Integer
Dim Year As Integer


Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll

'Horizontal Scroll Bar gives the day of the month'

Dim HScrollBar As Integer
HScrollBar = HScrollBar1.Value
txtDay.Text = HScrollBar

End Sub

Private Sub txtYear_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtYear.TextChanged

'Only 4 digit years between 1900 and 2100 are allowed

Year = txtYear.Text
If Year < 1900 Or Year > 2100 Then MsgBox(" Please enter a year between 1900 and 2100 ")

End Sub


Private Sub btnFindDay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDay.Click

'Match variables to user input

Month = cbMonth.Text
Day = txtDay.Text

'Assign numeric values to combo box items'

Select Case Month
Case "Jan"
MonthCase = 1
Case "Feb"
MonthCase = 2
Case "Mar"
MonthCase = 3
Case "Apr"
MonthCase = 4
Case "May"
MonthCase = 5
Case "Jun"
MonthCase = 6
Case "Jul"
MonthCase = 7
Case "Aug"
MonthCase = 8
Case "Sep"
MonthCase = 9
Case "Oct"
MonthCase = 10
Case "Nov"
MonthCase = 11
Case "Dec"
MonthCase = 12

End Select

'Algorithm to use for months January or February: calculates a value 0 to 6 for Saturday to Friday

If Month = "Jan" Or Month = "Feb" Then

dayNumber = ((Day + (2 * MonthCase) + (0.6 * (MonthCase + 1)) _
+ Year + (Year / 4) - (Year / 100) + ((Year / 400) + 2)) Mod 7) - 1

Else

'Algorithm to use for months March to December: calculates a value 0 to 6 for Saturday to Friday

dayNumber = (Day + (2 * (MonthCase + 12)) + (0.6 * (MonthCase + 13)) _
+ (Year - 1) + ((Year - 1) / 4) - ((Year - 1) / 100 + (((Year - 1) / 400) + 2))) Mod 7

End If

'Tell the user what day it is from the date entered

If dayNumber = 0 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Saturday ")
If dayNumber = 1 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Sunday ")
If dayNumber = 2 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Monday ")
If dayNumber = 3 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Tuesday ")
If dayNumber = 4 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Wednesday ")
If dayNumber = 5 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Thursday ")
If dayNumber = 6 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Friday ")

End Sub

'Exit Program

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

End

End Sub
End Class
Jul 15 '07 #5
pureenhanoi
175 100+
great, thanks all. this is a DateDiff function which i want to use e.g:

Private Function IsPPOrF(ByVal strDate As String) As String
If Not IsDate(strDate) Then
IsPPOrF = "Bad date value"

Else
Select Case DateDiff("d", Now, strDate)
Case 0
IsPPOrF = "Present"
Case Is < 0
IsPPOrF = "Past"
Case Is > 0
IsPPOrF = "Future"
End Select
End If
End Function

how would i add that to my code (which is awful but just about works) so my msgbox says "the date on the 14 jun 1977 is a friday past/present/future"? (it is down at the bottom)

Public Class Gregorian_Calendar_2

'Declare variables

Dim Day As Integer
Dim dayNumber As Integer
Dim Month As String
Dim MonthCase As Integer
Dim Year As Integer


Private Sub HScrollBar1_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles HScrollBar1.Scroll

'Horizontal Scroll Bar gives the day of the month'

Dim HScrollBar As Integer
HScrollBar = HScrollBar1.Value
txtDay.Text = HScrollBar

End Sub

Private Sub txtYear_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtYear.TextChanged

'Only 4 digit years between 1900 and 2100 are allowed

Year = txtYear.Text
If Year < 1900 Or Year > 2100 Then MsgBox(" Please enter a year between 1900 and 2100 ")

End Sub


Private Sub btnFindDay_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFindDay.Click

'Match variables to user input

Month = cbMonth.Text
Day = txtDay.Text

'Assign numeric values to combo box items'

Select Case Month
Case "Jan"
MonthCase = 1
Case "Feb"
MonthCase = 2
Case "Mar"
MonthCase = 3
Case "Apr"
MonthCase = 4
Case "May"
MonthCase = 5
Case "Jun"
MonthCase = 6
Case "Jul"
MonthCase = 7
Case "Aug"
MonthCase = 8
Case "Sep"
MonthCase = 9
Case "Oct"
MonthCase = 10
Case "Nov"
MonthCase = 11
Case "Dec"
MonthCase = 12

End Select

'Algorithm to use for months January or February: calculates a value 0 to 6 for Saturday to Friday

If Month = "Jan" Or Month = "Feb" Then

dayNumber = ((Day + (2 * MonthCase) + (0.6 * (MonthCase + 1)) _
+ Year + (Year / 4) - (Year / 100) + ((Year / 400) + 2)) Mod 7) - 1

Else

'Algorithm to use for months March to December: calculates a value 0 to 6 for Saturday to Friday

dayNumber = (Day + (2 * (MonthCase + 12)) + (0.6 * (MonthCase + 13)) _
+ (Year - 1) + ((Year - 1) / 4) - ((Year - 1) / 100 + (((Year - 1) / 400) + 2))) Mod 7

End If

'Tell the user what day it is from the date entered

If dayNumber = 0 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Saturday ")
If dayNumber = 1 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Sunday ")
If dayNumber = 2 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Monday ")
If dayNumber = 3 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Tuesday ")
If dayNumber = 4 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Wednesday ")
If dayNumber = 5 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Thursday ")
If dayNumber = 6 Then MsgBox(" The day on the " & Day & " " & Month & " " & Year & " is a Friday ")

End Sub

'Exit Program

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

End

End Sub
End Class
Sorry to say this. But its a complicated program to do a very small work. Try make something thats not such boring. People said that: everything is begining at zero, but i think thats an invalid statement.
Jul 16 '07 #6
darni
1
hey hi i too faced the same problem..
but i got the soln to get the day wen u type in the date with month and year....
to be frank i got the logic from the agarwal's quantitative aptitude book..
like u first take mod for the y-1... then subtract the result from the value of y-1
... u get two gr8 values a and b say... now perform this in both the values....
1) divide b (say) by 4-say c

2)b=b-c(now)

3)this is to calculate the number of odd days in the year....o1=2*c+b

4)if the number is greater than 7 then perform mod 7 of ans and get hte result...

5) do the same 1-4 procedure in a also...

6)add both the odd num of days...

7)perform mod 7 to get the exact odd days....

8)count the number of days from day 1 of the year till ur i/p date....

9)add it to odd num of days perform mod 7 and get the final answer....
u cud even simplify it... refer to r.s.agarwal's aptitude book u can get it cleared....
use switch case from 0-6 starting from sun to sat... and end up with ur desired result...
all the best..!
Jul 16 '07 #7
pureenhanoi
175 100+
hey hi i too faced the same problem..
but i got the soln to get the day wen u type in the date with month and year....
to be frank i got the logic from the agarwal's quantitative aptitude book..
like u first take mod for the y-1... then subtract the result from the value of y-1
... u get two gr8 values a and b say... now perform this in both the values....
1) divide b (say) by 4-say c

2)b=b-c(now)

3)this is to calculate the number of odd days in the year....o1=2*c+b

4)if the number is greater than 7 then perform mod 7 of ans and get hte result...

5) do the same 1-4 procedure in a also...

6)add both the odd num of days...

7)perform mod 7 to get the exact odd days....

8)count the number of days from day 1 of the year till ur i/p date....

9)add it to odd num of days perform mod 7 and get the final answer....
u cud even simplify it... refer to r.s.agarwal's aptitude book u can get it cleared....
use switch case from 0-6 starting from sun to sat... and end up with ur desired result...
all the best..!
Ouch!!!!!
Nice idea
Jul 17 '07 #8
Killer42
8,434 Expert 8TB
We may have overlooked some simpler options here (unless the required range of 1600 to 2100 isn't acceptable to VB - I haven't checked that).

Here's a code module (VB6) containing two functions to convert a date to a weekday, and a day, month and year to a weekday. As you can see from the code, you don't need a special function, since they both just invoke the Format() function to do the work.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Public Function Date2Weekday(ByVal parmDate As Date) As String
  4.   Date2Weekday = Format$(parmDate, "ddd")
  5. End Function
  6.  
  7. Public Function DDMMYYYY2Weekday(ByVal parmDD As Byte, ByVal parmMM As Byte, ByVal parmYYYY As Integer) As String
  8.   DDMMYYYY2Weekday = Format$(DateSerial(parmYYYY, parmMM, parmDD), "ddd")
  9. End Function
Jul 17 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Jann Rosendahl | last post: by
2 posts views Thread by Robert Oschler | last post: by
4 posts views Thread by Wanhua Yi | last post: by
3 posts views Thread by wbmca | last post: by
2 posts views Thread by WardB | last post: by
3 posts views Thread by Richard | last post: by
reply views Thread by brahimbb17 | last post: by
2 posts views Thread by MrBee | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.