[英]Adding months to a date value in vb.net
我正在嘗試下面的代碼,並獲得sql日期時間溢出異常.....問題出在黃金部分...其余部分即銀和高級工作正常....在黃金部分時,我嘗試添加某些日期時間字段的月數,我不知道會執行什么操作,因為通過消息框對其進行檢查時,在mem_date值中添加6個月后,ren_date的值為“ 12:00:00 AM”,而不是新的日期值....
Dim ren_date, mem_date As Date
Dim renmon As String
mem_date = TxtMemDate.Text
' checks the type of membership and adds corresponding number of years to the membership date and stores in renewal date
If ComboBox1.SelectedItem = "Silver" Then
ren_date = mem_date.AddMonths(3)
renmon = ren_date.ToString("MMMM")
ElseIf ComboBox1.SelectedItem = "Premium" Then
ren_date = mem_date.AddYears(1)
renmon = ren_date.ToString("MMMM")
ElseIf ComboBox1.SelectedItem = "Gold" Then
ren_date = mem_date.AddMonths(6)
renmon = ren_date.ToString("MMMM")
End If
MsgBox(mem_date)
MsgBox(ren_date)
我懷疑您的問題實際上在這里:
mem_date = TxtMemDate.Text
您嘗試在此處強制轉換為Date
的String
的格式可能不正確。 嘗試在此行之后放置一個斷點,然后通過調試器進行實驗。
這是另一種嘗試:也許您的ComboBox1.SelectedItem
與字符串“ Gold”不完全匹配。 在您的If
添加一個Else
塊,然后放入類似
MsgBox("*" & ComboBox1.SelectedItem.ToString() & "*")
例如,這應該顯示項目文本周圍是否有前導或尾隨空格。
該代碼按預期執行
Dim ren_date, mem_date As DateTime
Dim renmon As String = ""
If DateTime.TryParse(TxtMemDate.Text, mem_date) Then 'convert text to date
' checks the type of membership and adds corresponding number of years to the membership date and stores in renewal date
If ComboBox1.SelectedItem.ToString = "Silver" Then
ren_date = mem_date.AddMonths(3)
renmon = ren_date.ToString
ElseIf ComboBox1.SelectedItem.ToString = "Premium" Then
ren_date = mem_date.AddYears(1)
renmon = ren_date.ToString
ElseIf ComboBox1.SelectedItem.ToString = "Gold" Then
ren_date = mem_date.AddMonths(6)
renmon = ren_date.ToString
End If
Debug.WriteLine(renmon)
Else
Debug.WriteLine("Date entered is not correct format")
End If
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.