计算日期之间的年数和月数 [英] Counting years and months between dates

查看:104
本文介绍了计算日期之间的年数和月数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

朋友们,我有一个包含四个字段的表单,date1,date2,date3和date4。


所有这些都具有mm / dd / yyyy格式并且其源代码为a

表。


我需要添加一个未绑定的控件(我将其命名为txtresult),它将在几个月内计算
年份之间的差异。


Ex。

date1:​​01/01/1970

date2:05/05 / 1980年


date3:06/06/1990

date4:06/30/2001


txtresult :. ....


字段date3和date4可能为空,因此代码应该考虑

此选项仅计算前两个。


感谢您的帮助。

Friends, I have a form with four fields, date1, date2, date3 and date4.

All these have all a mm/dd/yyyy format and have their source to a
table.

I need to add an unbound control (I will name it txtresult) which will
count in months and years the difference between the dates.

Ex.
date1: 01/01/1970
date2: 05/05/1980

date3: 06/06/1990
date4: 06/30/2001

txtresult: .....

Fields date3 and date4 may be blank, therefore the code should consider
this option in counting only the first two.

Thanks for any help.

推荐答案

" jpr" < JP *** @ tin.it>在消息中写道

news:11 ********************* @ z14g2000cwz.googlegro ups.com ...
"jpr" <jp***@tin.it> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
朋友们,我有一个包含四个字段的表单,date1,date2,date3和date4。

所有这些都具有mm / dd / yyyy格式并且其来源为
表。

我需要添加一个未绑定的控件(我将其命名为txtresult),它将在几个月和几年内计算日期之间的差异。

Ex ............................................................. txtresult:.....

字段date3和date4可能是空白的,因此代码应该考虑
这个选项只计算前两个。 />
感谢您的帮助。
Friends, I have a form with four fields, date1, date2, date3 and date4.

All these have all a mm/dd/yyyy format and have their source to a
table.

I need to add an unbound control (I will name it txtresult) which will
count in months and years the difference between the dates.

Ex.
date1: 01/01/1970
date2: 05/05/1980

date3: 06/06/1990
date4: 06/30/2001

txtresult: .....

Fields date3 and date4 may be blank, therefore the code should consider
this option in counting only the first two.

Thanks for any help.



创建一个子来显示此间隔,就像您之前的帖子一样,您需要

从中调用它OnCurrent活动以及任何可以更改

日期的地方。这是一种事情:


私人SubInterval()


错误GoTo Err_Handler


Dim lngDays As Long

Dim lngMonths As Long

Dim lngYears As Long

Dim strInterval As String

Dim dteOne As Date

Dim dteTwo As Date


如果不是IsNull(Me.txtDate4)那么

如果IsNull(我。 txtDate3)然后

strInterval ="日期3缺失>

否则

dteOne = Me.txtDate3.Value

dteTwo = Me.txtDate4.Value

如果dteOne> dteTwo然后

strInterval ="日期四是在日期三之前

结束如果

结束如果

Else

如果是IsNull(Me.txtDate1)那么

如果是IsNull(Me.txtDate2)那么

strInterval ="日期1和2都缺失了;

Else

strInterval ="日期1缺失 

结束如果

Else

如果是IsNull(Me.txtDate2)那么

strInterval ="日期1缺失>

否则

dteOne =我。 txtDate1.Value

dteTwo = Me.txtDate2.Value

如果dteOne> dteTwo然后

strInterval ="日期二是在第一天之前

结束如果

结束如果

结束如果

结束如果


如果Len(strInterval)= 0那么


lngMonths = DateDiff(" m" ;,dteOne,dteTwo)


lngDays = DateDiff(" d",DateAdd(" m",lngMonths,dteOne),dteTwo)


如果lngDays< 0然后

lngMonths = lngMonths - 1

lngDays = DateDiff(" d",DateAdd(" m",lngMonths,dteOne),dteTwo)

结束如果


lngYears = lngMonths \ 12


lngMonths = lngMonths Mod 12


strInterval = CStr(lngYears)& " yrs &安培; _

CStr(lngMonths)& " mths &安培; _

CStr(lngDays)& "天


结束如果


Me.txtResult = strInterval


Exit_Handler:

退出Sub


Err_Handler:

MsgBox Err.Description,vbExclamation,"错误号:" &安培; Err.Number

简历Exit_Handler


结束子


Create a sub to show this interval and like your previous post, you need to
call it from the OnCurrent event and also any place where you can catch the
dates changing. Here is the sort of thing:

Private Sub ShowInterval()

On Error GoTo Err_Handler

Dim lngDays As Long
Dim lngMonths As Long
Dim lngYears As Long
Dim strInterval As String
Dim dteOne As Date
Dim dteTwo As Date

If Not IsNull(Me.txtDate4) Then
If IsNull(Me.txtDate3) Then
strInterval = "Date 3 is missing"
Else
dteOne = Me.txtDate3.Value
dteTwo = Me.txtDate4.Value
If dteOne > dteTwo Then
strInterval = "Date four is before date three"
End If
End If
Else
If IsNull(Me.txtDate1) Then
If IsNull(Me.txtDate2) Then
strInterval = "Dates 1 and 2 are missing"
Else
strInterval = "Date 1 is missing"
End If
Else
If IsNull(Me.txtDate2) Then
strInterval = "Date 1 is missing"
Else
dteOne = Me.txtDate1.Value
dteTwo = Me.txtDate2.Value
If dteOne > dteTwo Then
strInterval = "Date two is before date one"
End If
End If
End If
End If

If Len(strInterval) = 0 Then

lngMonths = DateDiff("m", dteOne, dteTwo)

lngDays = DateDiff("d", DateAdd("m", lngMonths, dteOne), dteTwo)

If lngDays < 0 Then
lngMonths = lngMonths - 1
lngDays = DateDiff("d", DateAdd("m", lngMonths, dteOne), dteTwo)
End If

lngYears = lngMonths \ 12

lngMonths = lngMonths Mod 12

strInterval = CStr(lngYears) & " yrs " & _
CStr(lngMonths) & " mths " & _
CStr(lngDays) & " days"

End If

Me.txtResult = strInterval

Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler

End Sub


我能说什么!刚刚好!打算工作。按照我想要的方式工作,

only ,,,,我想删除这些日子。因为我的情况不需要

。我可以只显示yy和mm吗?谢谢。

What can I say! Just perfect! Greate job.Working the way I want,
only,,,,I would like to remove the days. Since it is not necessary for
my case. Can I just show the yy and mm? Thanks.


忘了重要的细节。我已将您的代码添加到表单的Open

事件中,因为OnCurrect已经有代码。


我还需要相同的代码用于另一组控制意义

我需要为其他领域重复相同的操作。我在哪里放了

代码(据我所知,我只需用

新代码更改txtnames)。谢谢。

Just forgot an important detail. I have added your code to the Open
event of the form since the OnCurrect already has code.

I will also need the same code for another group of controls meaning
that I will need to repeat the same for other fields. Where do I put
the code (I understand that i will simply change the txtnames with the
new ones). Thanks.


这篇关于计算日期之间的年数和月数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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