将一年添加到dateTimePicker [英] Add a year to a dateTimePicker

查看:57
本文介绍了将一年添加到dateTimePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个字段,即txtTerm,dtCommencementDate和dtDateOfExpiry。现在,dtCommencementDate和dtDateOfExpiry是DateTimePickers。我希望用户输入例如99,然后应将其添加到纪念日以确定到期日期。下面是代码,但是DateTimePicker的dtDateofExpiry没有添加。请帮忙。



  Dim  intTerm 正如 整数 = Val( Me  .txtTerm.Text)
Dim dtComm 作为 日期 = .dtCommencementDate.Text
Dim dtExp As Date = DateAdd(DateInterval.Year,intTerm,dtComm)
Me .dtDateOfExpiry.Text = dtExp

解决方案

您没有显示您使用的 DateTimePicker ,但是它很可能具有 System.DateTime 类型的值,因此您可以使用 System.DateTime.AddYears

https://msdn.microsoft.com/en-us/library/system.datetime.addyears(V = vs.110)的.aspx [ ^



-SA


你应该设置 [ ^ ]而不是文字属性!

<前lang =vb> .dtDateOfExpiry.Value = DateTime.Now.AddYears( 1


非常感谢,但在调试过程中,我可以看到年份被添加到到期日,但在表格上没有变化。而不是将年份添加到它,而是减少它。当我将99年添加到今天的日期时,它减少了一年,即10月27日至10月14日。我只是不知道为什么我已经尝试了所有代码。


Please I have three fields, namely txtTerm, dtCommencementDate and dtDateOfExpiry. Now, dtCommencementDate and dtDateOfExpiry are DateTimePickers. I want if the user enters the term e.g. 99, then it should be added to the date of commecmcement to determine the date of expiry. Below is the code, but the dtDateofExpiry which is the DateTimePicker is not adding. Please help.

Dim intTerm As Integer = Val(Me.txtTerm.Text)
           Dim dtComm As Date = Me.dtCommencementDate.Text
           Dim dtExp As Date = DateAdd(DateInterval.Year, intTerm, dtComm)
           Me.dtDateOfExpiry.Text = dtExp

解决方案

You did not show which DateTimePicker you use, but most likely it has the value of the type System.DateTime, so you can use System.DateTime.AddYears:
https://msdn.microsoft.com/en-us/library/system.datetime.addyears(v=vs.110).aspx[^].

—SA


You should set Value[^] instead of Text property!

Me.dtDateOfExpiry.Value = DateTime.Now.AddYears(1)


Many thanks but during debugging, I can see the year is being added to the expiry date but does not change on the form. Instead of adding the year to it, it rather decreases it. when I add 99 years to today's date, it decreases by one year, thus 27-Oct-14. I just don't know why though I have tried all your codes.


这篇关于将一年添加到dateTimePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆