VBScript DateDiff month

守給你的承諾、 提交于 2019-12-02 10:02:59

问题


I am having a problem about getting the month datedifference between two dates. Here is a sample:

DateDiff("m","2014-10-17","2014-10-30")

The above code returns 0 months since it is less than a month. But,

DateDiff("m","2014-10-17","2014-11-01")

returns 1 which should not be since it is still 15 days.

My problem is I want to see if these two dates already exceed a month but it seems that it calculates 1 month only when the month part of the date is changed.


回答1:


DateDiff calculates the span between two timestamps with the precision defined in the first parameter. From what you described in your question and comments you rather seem to be looking for something like this:

ds1 = "2014-10-17"
ds2 = "2014-10-30"

d1 = CDate(ds1)
d2 = CDate(ds2)

diff = DateDiff("m", d1, d2)
If diff > 0 And Day(d2) < Day(d1) Then diff = diff - 1

WScript.Echo diff & " full months have passed between " & ds1 & " and " & ds2 & "."



回答2:


You can calculate day difference between two dates using 'DateDiff("d",date1,date2)'. Then calculate number of full-month by dividing 30 days.

Following is an example.

monthDiff = int(DateDiff("d","2014-10-17","2014-11-01") / 30)

As you know, days of months differs by month. For example, November has 30 days and December has 31 days.

If you wish to use 30 days a month when first day is on November, and to use 31 days a month whe first day is on December, the code need a table of days of months and routine to handle month information.



来源:https://stackoverflow.com/questions/27030073/vbscript-datediff-month

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!