VBScript DateDiff month

前端 未结 2 1331
半阙折子戏
半阙折子戏 2021-01-24 01:06

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\")

2条回答
  •  猫巷女王i
    2021-01-24 01:23

    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 & "."
    

提交回复
热议问题