VBA/Excel/Access/Word/Date Functions/DateDiff

Материал из VB Эксперт
Перейти к: навигация, поиск

DateDiff() function returns the difference between two dates. The unit for the difference is specified as a string ( "s" for second, "m" for month, "yyyy" for year, and so on).

 
Sub dateDiffDemo()
    Debug.Print DateDiff("s", Now, "10/10/03")
End Sub



DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) returns the interval between two specified dates

 
Constant         Value  Year Starts with Week         
vbUseSystem      0      Use the system setting.        
vbFirstJan1      1      The week in which January 1 falls (the default setting).        
vbFirstFourDays  2      The first week with a minimum of four days in the year.        
vbFirstFullWeek  3      The first full week (7 days) of the year.



DateDiff("m", "0", Now)

 
Sub dateFunctions1()
   Debug.Print "The months between 3/15/2000 and today is: " & DateDiff("m", "3/15/2000", Now)
End Sub



DateDiff("m", Now, ")

 
Sub dateDiff2()
    Debug.Print DateDiff("m", Now, "10/10/03")
End Sub



DateDiff returns the interval of time between two dates:

 
Sub DateDiffExample()
  Debug.Print DateDiff("d", Now, "12/31/2010")
  ""Days until 12/31/2010
  Debug.Print DateDiff("m", Now, "12/31/2010")
  ""Months until 12/31/2010
  Debug.Print DateDiff("yyyy", Now, "12/31/2010")
  ""Years until 12/31/2010
  Debug.Print DateDiff("q", Now, "12/31/2010")
  ""Quarters until 12/31/2010
End Sub



DateDiff("yyyy", Now, ")

 
Sub dateDiff3()
    Debug.Print DateDiff("yyyy", Now, "10/10/03")
End Sub



Difference between two dates in days and months:

 
Sub dateFunctions()
   Dim strDateString As String
   strDateString = "The days between 3/15/2000 and today is: " & _
                    DateDiff("d", "3/15/2000", Now) & vbCrLf & _
                   "The months between 3/15/2000 and today is: " & _
                    DateDiff("m", "3/15/2000", Now)
   msgBox strDateString
End Sub



returns the number of weeks between June 3, nd September:

 
Sub dateD()
    MsgBox DateDiff("ww", "6/3/2006", "9/30/2006")
End Sub
"Using the Dir Function to Check Whether a File Exists
  Sub Does_File_Exist()
      Dim strTestFile As String, strNameToTest As String
      strNameToTest = "c:\"
      If strNameToTest = "" Then End
      strTestFile = Dir(strNameToTest)
      If Len(strTestFile) = 0 Then
          Debug.Print "The file " & strNameToTest & " does not exist."
      Else
          Debug.Print "The file " & strNameToTest & " exists."
      End If
  End Sub



Use DateDiff function

 
Sub dateDiffFunction()
   Dim strDateString As String
   
   strDateString = "The days between 3/15/2000 and today is: " & _
                   DateDiff("d", "3/15/2000", Now) & vbCrLf & _
                   "The months between 3/15/2000 and today is: " & _
                   DateDiff("m", "3/15/2000", Now)
   
   MsgBox strDateString
End Sub