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).

   <source lang="vb">

Sub dateDiffDemo()

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

End Sub

</source>
   
  


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

   <source lang="vb">

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.

</source>
   
  


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

   <source lang="vb">

Sub dateFunctions1()

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

End Sub

</source>
   
  


DateDiff("m", Now, ")

   <source lang="vb">

Sub dateDiff2()

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

End Sub

</source>
   
  


DateDiff returns the interval of time between two dates:

   <source lang="vb">

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

</source>
   
  


DateDiff("yyyy", Now, ")

   <source lang="vb">

Sub dateDiff3()

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

End Sub

</source>
   
  


Difference between two dates in days and months:

   <source lang="vb">

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

</source>
   
  


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

   <source lang="vb">

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
</source>
   
  


Use DateDiff function

   <source lang="vb">

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

</source>