Results 1 to 12 of 12

Thread: [2008] Checking for a specific date

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Question [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

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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:
    1. Dim nowDate As Date = Date.Now
    2.         Dim searchDate As Date = New Date(2007, 12, 15)
    3.         Dim futureDate As Date = nowDate.AddDays(7)
    4.         If nowDate <= searchDate AndAlso futureDate >= searchDate Then
    5.  
    6.         End If
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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:
    1. Dim bDay As Date = "3 / 1"
    2. If DateDiff(DateInterval.Day, Today, bDay) < 0 Then
    3.    MsgBox(DateDiff(DateInterval.Day, Today, bDay.AddYears(1)) & " days until " & bDay.AddYears(1))
    4. Else
    5.    MsgBox(DateDiff(DateInterval.Day, Today, bDay) & " days until " & bDay)
    6. End If

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Checking for a specific date

    try this:

    vb Code:
    1. Dim x As Integer = 0
    2. Dim birthday() As Integer = {4}
    3. Dim BMonthNB() As Integer = {1}
    4. Dim bDay As Date = birthday(x) & " / " & BMonthNB(x)
    5. MsgBox(bDay.ToString)

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Checking for a specific date

    if it doesn't work, try this

    vb Code:
    1. Dim x As Integer = 0
    2. Dim birthday() As Integer = {4}
    3. Dim BMonthNB() As Integer = {1}
    4. Dim bDay As Date = ctype(birthday(x) & " / " & BMonthNB(x), date)
    5. MsgBox(bDay.ToString)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    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

  9. #9
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    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.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Dec 2007
    Posts
    167

    Re: [2008] Checking for a specific date

    to answer SH, i mean what u said
    how can I convert 2 strings to a date?

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  12. #12
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
  •  



Click Here to Expand Forum to Full Width