I am a new to vb.net and daniweb

I am working with an application (A Small game) in which on special days, there is a sale in the game market, which is with game money.
For it, I Have a boolean data type in the settings (My.mysettings) named special. I Also have a form named 'SpecialDay'. When I
Open my application on those special days, the special day form would come. Plz. See this code:

               If Now.Day = 25 And Now.Month = 12 Then
                    specialday.Label3.Text = "For Celebrating Christmas"
                    settings.special = True
                ElseIf Now.Day = 1 And Now.Month = 1 Then
                    specialday.Label3.Text = "For Celebrating New Year!"
                    settings.special = True
                ElseIf Now.DayOfWeek = 7 And Now.Month = 5 And Now.week = 2 Then
                    specialday.Label3.Text = "For Celebrating Mother's Day"
                    settings.special = True
                    settings.special = False
                End If

The first 2 of them worked perfectly. I Also want Mother's day and Father's Day. I Tried to do it, by defining Date, Month, week but an Error comes in the week. Plz. Anyone help me.

Recommended Answers

All 6 Replies

You want the 7th day of the 2nd week of Month MAY right
You can use it like this then

ElseIf Now.Month = 5 AndAlso Now.Day = 14 Then
                    specialday.Label3.Text = "For Celebrating Mother's Day"
                    settings.special = True

This is not working.
I want for 2nd Sunday of the month not 14 May.

If you know what day was May 1st, you can calculate which day is Mother's Day. For example, if May 1st is a sunday, the day number you are looking for is 8.

There is no WeekOfMonth directly available, although you could create one.

Its very simple actually to create an algorithm for that. Since you already know that its always the second sunday of the month then what you need is to get the first day of the month and determine what days is it. From there you will know how many days you need to add to get the target date.

Below is my sample.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim thisDate As Date = GetSecondSundayOfTheMonth(Date.Today.Month, Date.Today.Year)
    End Sub

    Function GetSecondSundayOfTheMonth(ByVal month As Integer, ByVal year As Integer)
        Dim firstDayOfTheMonth As Date = CDate(String.Format("{0}/1/{1}", month, year))

        Select Case firstDayOfTheMonth.DayOfWeek
            Case DayOfWeek.Monday
                Return DateAdd(DateInterval.Day, 13, firstDayOfTheMonth)
            Case DayOfWeek.Tuesday
                Return DateAdd(DateInterval.Day, 12, firstDayOfTheMonth)
            Case DayOfWeek.Wednesday
                Return DateAdd(DateInterval.Day, 11, firstDayOfTheMonth)
            Case DayOfWeek.Thursday
                Return DateAdd(DateInterval.Day, 10, firstDayOfTheMonth)
            Case DayOfWeek.Friday
                Return DateAdd(DateInterval.Day, 9, firstDayOfTheMonth)
            Case DayOfWeek.Saturday
                Return DateAdd(DateInterval.Day, 8, firstDayOfTheMonth)
            Case DayOfWeek.Sunday
                Return DateAdd(DateInterval.Day, 7, firstDayOfTheMonth)
        End Select

    End Function

Set Date.day = 1 and loop using Date.AddDays(1) until Date.DayOfWeek = DayOfWeek.sunday the second time. Hope this helps, have not time to work this out right now myself. Also have a look here for more info.

Here is how I handeled holidays

Dim dDate As Date
 dDate = DateTime.Parse(dDate, Globalization.CultureInfo.CreateSpecificCulture("en-CA"))
 Dim blnCoerceDateSucceeded As Boolean
 dDate = CDate(DateIn)
  If Weekday(DateIn) = FirstDayOfWeek.Saturday Or Weekday(DateIn) = FirstDayOfWeek.Sunday Then
   Exit Function
 If Month(dDate) = 1 Then
 If VB.Day(dDate) = 1 Or (WeekDay(dDate) = FirstDayOfWeek.Monday And VB.Day(dDate) = 2) Then
FederalHoliday = True 
End If
If WeekDay(dDate) = FirstDayOfWeek.Monday And VB.Day(dDate) > 14 And VB.Day(dDate) < 22 Then
FederalHoliday = True 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.