Day Finder Program

anatomycut
07-13-2007, 09:23 AM
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 to date), kind regards

AtmaWeapon
07-13-2007, 09:50 AM
Well it sounds like a school assignment but I think I can provide guidance.

First, TextBox controls may not be the best idea in this case. Consider using DateTimePickers for date entry, since they return a DateTime value.

DateTime has a Now property that represents the moment in time when you access the property. It also overrides the > and < operators, so if Date1 < Date2 you know Date1 happened before Date2. Finally, DateTime has a DayOfWeek property that indicates what day of the week a particular date was.

anatomycut
07-15-2007, 09:34 AM
great, thanks. i want to add a DateDiff Function to my code (which is awful but just about works. i'm using Vb 2005 express edition) 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

i have 2 text boxes, a combo box, a find day button and a exit button. how do i do it so my msgbox (it is down at the bottom) says e.g. the day on the 14 jun 1977 is a friday past/present/future. any suggestions please?

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

MKoslof
07-15-2007, 10:20 AM
In .Net the Days of the Week are stored in an enumeration, so you can simply use the .DayOfWeek method to return the day in the week that this particular day occurs on.

A past post regarding some date manipulation, day of week processing:

http://www.xtremevbtalk.com/showthread.php?t=284127

AtmaWeapon
07-15-2007, 12:22 PM
In .Net the Days of the Week are stored in an enumeration, so you can simply use the .DayOfWeek method to return the day in the week that this particular day occurs on.
You know it's almost like I mentioned exactly that in a previous post but he completely overlooked the post and reimplemented the parts he could find code for through Google, then asked for the code for the parts that weren't found through Google!

anatomycut, here are answers to your questions copied verbatim from my last post:
how do i do it so my msgbox (it is down at the bottom) says e.g. the day on the 14 jun 1977 is a friday past/present/future. any suggestions please?DateTime has a Now property that represents the moment in time when you access the property. It also overrides the > and < operators, so if Date1 < Date2 you know Date1 happened before Date2.
*50 or so lines of code to see what day of the week a day was*Finally, DateTime has a DayOfWeek property that indicates what day of the week a particular date was.

Part of programming in .NET is reusing components that have been created and tested by others so that you don't have to write the code yourself. Microsoft has provided a DateTime structure that provides many functions for interacting with Dates and Times, including methods that determine the day of the week a particular date fell on and whether a particular Date instance is earlier or later than another instance.

You can continue using individual variables for Day, Month, and Year to represent dates. But then you have to re-implement DateTime.Compare and DateTime.DayOfWeek yourself, and date arithmetic is pretty tricky.

Also I believe you need to define "present", as it is more a concept than an actual point in time. In general, to have DateTime1 = DateTime2 return true requires some manipulation of the variables, since the Ticks property almost always has some variance.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum