James Swan
James Swan

Reputation: 81

How to add 1 month to selected DateTimePicker

I'm having an issue with DateTimePicker.

What I am currently trying to do is based off of what text is in lblPrevSem(Previous Semester), which is getting its selection from a drop down on a previous screen, i want to add a certain amount of time to the DateTimePicker.

Public Property CustomFormat As String
Dim SemesterMonths As Integer
Dim SemesterDays As Integer


Private Sub DeptCreatePrevSch_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim SemesterYear() As String = DeptPrevSch.CboSem.Text.Split(",")


    lblPrevSem.Text = SemesterYear(0)
    cboYear.Text = Date.Now.Year
    For i As Integer = 0 To 5
        cboYear.Items.Add(Date.Now.Year + i)
        cboYear.SelectedIndex = 0
    Next

    If InStr(lblPrevSem.Text, "Fall") Then
        SemesterMonths = 1
    ElseIf InStr(lblPrevSem.Text, "Spring") Then
        SemesterMonths = 1
    ElseIf InStr(lblPrevSem.Text, "Summer") Then
        SemesterDays = 14
    End If

    Call dtpStart_ValueChanged(sender, e)
End Sub

Private Sub dtpStart_ValueChanged(sender As Object, e As EventArgs) Handles dtpStart.ValueChanged

    Dim StartDate As Date
    Dim StartStringDate As String
    Dim EndDate As Date
    Dim EndStringDate As String


    dtpStart.Format = DateTimePickerFormat.Custom
    dtpStart.CustomFormat = "MMMM dd, yyyy  dddd"

    StartDate = dtpStart.Value.ToString
    StartStringDate = StartDate.ToString("MMMM dd, yyyy  dddd")
    lblRegStartDate.Text = StartStringDate

    EndDate = dtpStart.Value.AddMonths(SemesterMonths)
    EndDate = dtpStart.Value.AddDays(SemesterDays)
    EndStringDate = EndDate.ToString("MMMM dd, yyyy  dddd")
    lblRegEndDate.Text = EndStringDate

End Sub

I can get it to add in days just fine but when ever i try and add in 1 month, it doesn't seem to work at all.

I've tried multiple different ways to add in a 1 month but nothing so far has worked. The closet ive been was adding in 30 days but then that doesn't account for months that have 31 days.

Reg Start Date is what ever the DateTimePicker is and Reg End Date should be the added days based off of what lblPrevSem is

Both Reg Start/End Date are displayed as labels

(i.e. Fall = 1 Month, Spring = 1 Month, Summer = 2 Weeks)

Upvotes: 2

Views: 4073

Answers (2)

Creator
Creator

Reputation: 1512

Just get the datetimepicker value and put .AddYears(0) or .AddDays(0) or AddMonths(0) behind it.
But you can also use them all at the same time.

nextServiceDateTimePicker.Value.AddYears(0).AddDays(0).AddMonths(0);

Just replace the 0 with lets say i and give it the value you need it to be.

Upvotes: 1

NoAlias
NoAlias

Reputation: 9193

Your problem is that you are resetting the value for EndDate after adding the SemesterMonths value. You should add the SemesterDays value to the EndDate variable, not reset the value of EndDate with dtpStart.Value.AddDays(SemesterDays):

EndDate = dtpStart.Value.AddMonths(SemesterMonths)
EndDate = EndDate.AddDays(SemesterDays)

Upvotes: 1

Related Questions