如何使用datetimepicker计算一个日期与另一个日期之间的差异? [英] How can I calculate differences from one date to another with datetimepicker?
问题描述
您好,我是Visual Basic的初学者.我做了一些代码,引用了一种表格,需要计算出我的出生日期与另一个日期之间的差异.我做了一个声明,让我了解了年份之间的区别, 但是我需要知道如何才能把几个月和几天付诸东流.例子如果我的生日是1991年11月26日,并且我想计算到今天已经经历了多少年,几个月和几天,那将是20年3个月零11天.
Hi, Im a beginner at visual basic. I did some codes refering to a form in which I need to calculate the differences between my my date of birth to another date. I did a statement which gives me the difference between those in the format of years, but I need to know how I can put the months and days example if my birthdate is 11/26/1991 and I want to calculte how many years, month and days has transcurred until today it would be like 20 years 3 months and 11 days.
我的代码:
公共
Form1
ClassForm1
Sub Button1_Click( ByVal 发件人
PrivateSubButton1_Click(ByValsender AsSystem.Object, ByVale AsSystem.EventArgs) HandlesButton1.Click
If(TextBox1.Text.Length = 0) Then
MsgBox(
"Tiene que llenar la casilla de fecha de nacimiento",
MsgBoxStyle.Exclamation, )
TextBox1.Focus()
TextBox1.Focus()
Exit Sub
Else
Try
Dimfechanacimiento AsDate= Date.Parse(TextBox1.Text)
Catchex AsException
TextBox1.Clear()
TextBox1.Clear()
MsgBox(
& vbCr& & _
" Por favor, verifique que la fecha sea valida "& vbCr & " El formato es Ej. "& _
FormatDateTime(Now.Date,
FormatDateTime(Now.Date,
DateFormat.ShortDate), _
MsgBoxStyle.Critical, _
"Error con la fecha")
尝试
EndTry
如果
EndIf
Dimdate1 AsDate= TextBox1.Text
Dimdate2 AsDate= DateTimePicker1.Value
MsgBox(DateDiff(
MsgBox(DateDiff(
,date1,date2))
"yyyy", date1, date2))
Sub
EndSub
结束
推荐答案
请启用严格启用选项",更正错误,然后重试.还有一个插入代码"单击该编辑器窗口顶部的按钮.
Please enable Option Strict On, fix the errors and try again. There's also a "insert code" button at the top of this editor window.
这篇关于如何使用datetimepicker计算一个日期与另一个日期之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!