|
-
Dec 27th, 2007, 04:48 PM
#1
Thread Starter
Addicted Member
[2008] Checking for a specific date
I want to check if after one week from now is a specifc give date (for example is after one week 24/12? (DD/MM)
I am creating a simple birthday alarm program, it will check my data and tell me after one week is the birthday of bla.
here is my code, but its not working correctly
no errors are given, but its just not working:
Dim dt As DateTime = DateTime.Now
If (Val(BDay(x)) - dt.Day.ToString > 0 And Val(BDay(x)) - 7 = dt.Day.ToString And Val(BMonthNB(x)) = dt.Month.ToString) Or (Val(BDay(x)) - dt.Day.ToString < 0 And BMonth(x) = dt.AddMonths(CInt(-1)).ToString And 30 - Val(BDay(x)) - dt.Day.ToString) Then LstBirthday.Items.Add("After one week is the birthday of " & UserName(x))
BDay(x) is a string with the day number
BMonthNb(x) is a string with the month number
BMonth(x) is a string with the month name
this code is in a For Next loop
-
Dec 27th, 2007, 04:59 PM
#2
Re: [2008] Checking for a specific date
So, are you saying that you want to see if a certain date will occur between todays date and 7 days ahead? If so:
VB.Net Code:
Dim nowDate As Date = Date.Now
Dim searchDate As Date = New Date(2007, 12, 15)
Dim futureDate As Date = nowDate.AddDays(7)
If nowDate <= searchDate AndAlso futureDate >= searchDate Then
End If
-
Dec 27th, 2007, 05:13 PM
#3
Re: [2008] Checking for a specific date
this will get the number of days from today until the next occurence of "dd/M"
vb Code:
Dim bDay As Date = "3 / 1"
If DateDiff(DateInterval.Day, Today, bDay) < 0 Then
MsgBox(DateDiff(DateInterval.Day, Today, bDay.AddYears(1)) & " days until " & bDay.AddYears(1))
Else
MsgBox(DateDiff(DateInterval.Day, Today, bDay) & " days until " & bDay)
End If
-
Dec 28th, 2007, 11:10 AM
#4
Thread Starter
Addicted Member
Re: [2008] Checking for a specific date
both codes are perfect but are specific
@atheist
Code:
Dim searchDate As Date = New Date(ANYYEAR, Val(BMonthNB(x)), Val(bDay(x)))
If nowDate <= searchDate AndAlso futureDate >= searchDate Then
End If
i have 2 problems, first I dont have a specific year (its a birthday), second VB.Net wont allow me to put variables in Dim
how can I fix this?
remember all this code is in a For Next loop that checks many birthdays to see if its in the coming week
@.paul.
Code:
Dim bDay As Date = bDay(x) & " / " & BMonthNB(x)
same problem, i cant dim a variable
this code is in a For Next loop that checks many birthdays, I need to place variables
Thank You both
-
Dec 28th, 2007, 11:15 AM
#5
Re: [2008] Checking for a specific date
What do you mean when you say you can't dim a variable?
This code:
Dim bDay As Date = bDay(x) & " / " & BMonthNB(x)
won't work with option Strict Off, since it would be implicitly converting a string to a date, which would not be allowed.
You have a year, it is the year of the current date, so you might as well add it back in, but the main question is what do you mean by not being able to dim a variable?
My usual boring signature: Nothing
 
-
Dec 28th, 2007, 11:23 AM
#6
Re: [2008] Checking for a specific date
try this:
vb Code:
Dim x As Integer = 0
Dim birthday() As Integer = {4}
Dim BMonthNB() As Integer = {1}
Dim bDay As Date = birthday(x) & " / " & BMonthNB(x)
MsgBox(bDay.ToString)
-
Dec 28th, 2007, 11:25 AM
#7
Re: [2008] Checking for a specific date
if it doesn't work, try this
vb Code:
Dim x As Integer = 0
Dim birthday() As Integer = {4}
Dim BMonthNB() As Integer = {1}
Dim bDay As Date = ctype(birthday(x) & " / " & BMonthNB(x), date)
MsgBox(bDay.ToString)
-
Dec 28th, 2007, 11:51 AM
#8
Thread Starter
Addicted Member
Re: [2008] Checking for a specific date
Code:
Dim bnDay As Date = CType(Val(BDay(x)) & " / " & Val(BMonthNB(x)), Date)
i get Conversion from string "28 / 1" to type 'Date' is not valid.
Code:
Dim NewBDayVar() As Integer = Val(BDay(x))
Value of double cant be converted to 1-dimensional array of integer
plz help
-
Dec 28th, 2007, 11:59 AM
#9
Re: [2008] Checking for a specific date
There are several fundamental things you need to learn here.
You cant just assign a variable/constant of a certain type to a variable of another type. Also I'd suggest you read SH's post and answer his questions.
-
Dec 28th, 2007, 12:07 PM
#10
Thread Starter
Addicted Member
Re: [2008] Checking for a specific date
to answer SH, i mean what u said
how can I convert 2 strings to a date?
-
Dec 28th, 2007, 12:44 PM
#11
Re: [2008] Checking for a specific date
A date needs three parts: day, month, and year. Otherwise you don't really have a date.
Now, you stated that this is a birthday, which is a recurring event not tied to a specific year, but it seems that if you are talking about whether or not you are seven days away from a date (a reminder, as you mentioned), then you actually have a year: this year!
Therefore, what you would do to create a date from a string would be:
Dim bnDay As Date = CDate(CInt(BDay(x)) & " / " & CInt(BMonthNB(x)) & "/" & date.Now.Year)
Of course, this could be improved if BDay and BMonthNB were integers, such that the CInt conversion was not actually needed, but I assumed from your use of Val that they were strings currently. CInt will be a bit better than Val, though either one would actually be fine. The difference is merely aesthetics in this case. CDate will have a slight performance benefit over CType (I think), and it is somewhat easier to use, so it is somewhat preferred.
Adding in the current year, as I did, for the year portion, will work....in most cases. The problem is that if you are getting up around the end of the year, you may have to use year+1 to deal with the next year.
My usual boring signature: Nothing
 
-
Dec 28th, 2007, 04:52 PM
#12
Re: [2008] Checking for a specific date
Code:
Private Sub tstDT()
Dim dtN As Date = Now
Dim monthN As String = "1" 'month number as string
Dim dayN As String = "1" 'day of month as string
Dim BDstr As String
Dim dtBD As Date, whenD As TimeSpan, numDAYS As Integer
'force year of birth and time to NOW
BDstr = monthN & "/" & dayN & "/" & dtN.Year.ToString & " " & dtN.ToShortTimeString
Console.WriteLine("a " & BDstr)
dtBD = Date.Parse(BDstr)
whenD = dtBD - dtN
numDAYS = whenD.TotalDays
Console.WriteLine(numDAYS)
Select Case numDAYS
Case 0 To 7
Console.WriteLine("Within 7 days")
Case -364 To -358
Console.WriteLine("Within 7 days")
Case Else
Console.WriteLine("Better luck tomorrow")
End Select
End Sub
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|