如何将1个月添加到选定的DateTimePicker

时间:2015-12-07 15:40:08

标签: vb.net datetimepicker

我遇到了DateTimePicker的问题。

我目前正在尝试做的是基于lblPrevSem(上一学期)中的文本,它是从前一个屏幕上的下拉菜单中选择的,我想为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

我可以在几天内添加它,但是当我尝试在1个月内添加时,它似乎根本不起作用。

我已尝试过多种不同的方式在1个月内添加,但到目前为止还没有任何效果。衣柜我一直在30天内添加,但那并没有占到31天的月份。

注册开始日期是DateTimePicker的任何内容,注册结束日期应该是基于lblPrevSem

的添加天数

Reg Reg / End Date都显示为标签

(即秋季= 1个月,春季= 1个月,夏季= 2周)

2 个答案:

答案 0 :(得分:1)

您的问题是您在添加SemesterMonths值后重置EndDate的值。您应该将SemesterDays值添加到EndDate变量,而不是使用dtpStart.Value.AddDays(SemesterDays)重置EndDate的值:

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

答案 1 :(得分:1)

只需获取datetimepicker值并将.AddYears(0)或.AddDays(0)或AddMonths(0)置于其后面。
但你也可以同时使用它们。

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

只需用假设i替换0并给它你需要的值。