[英]Adding months to a date value in vb.net
I am trying the following code and getting the sql date time overflow exception.....The problem lies in the Gold part...rest to ie Silver and Premium work fine....When in Gold part I try to add certain number of months to a date time field I don't know what action is performed as when i check it through Message box the ren_date has value "12:00:00 AM" rather than the new date value after adding 6 months in mem_date value.... 我正在尝试下面的代码,并获得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)
I suspect your issue is actually here: 我怀疑您的问题实际上在这里:
mem_date = TxtMemDate.Text
It may be that the String
you're trying to coerce to a Date
here isn't in the right format. 您尝试在此处强制转换为
Date
的String
的格式可能不正确。 Try putting a breakpoint after this line and go through with a debugger to experiment. 尝试在此行之后放置一个断点,然后通过调试器进行实验。
Here's another thing to try: maybe your ComboBox1.SelectedItem
does not exactly match the string "Gold." 这是另一种尝试:也许您的
ComboBox1.SelectedItem
与字符串“ Gold”不完全匹配。 Add an Else
block to your If
and put in there something like 在您的
If
添加一个Else
块,然后放入类似
MsgBox("*" & ComboBox1.SelectedItem.ToString() & "*")
This should reveal if there are, for example, leading or trailing spaces around the item's text. 例如,这应该显示项目文本周围是否有前导或尾随空格。
This code performs as expected 该代码按预期执行
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.