Datediff
Datediff
Syntax DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]]) The DateDiff function syntax has these named arguments: Part interval Description Required. String expression that is the interval of time you use to calculate the difference between date1 and date2. Required; Variant (Date). Two dates you want to use in the calculation. Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed. Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.
firstweekofyear
Settings The Interval argument can have one of the following settings. Enumeration value String Unit of time difference DateInterval.Day DateInterval.DayOfYear DateInterval.Hour DateInterval.Minute DateInterval.Month DateInterval.Quarter DateInterval.Second DateInterval.Weekday d y h n m q s w Day Day Hour Minute Month Quarter Second Week
Remarks You can use the DateDiff function to determine how many specified time intervals exist between two dates. For example, you might use DateDiff to calculate the number of days between two dates, or the number of weeks between today and the end of the year. To calculate the number of days between date1 and date2, you can use either Day of year ("y") or Day ("d"). When interval is Weekday ("w"), DateDiff returns the number of weeks between the two dates. If date1 falls on a Monday, DateDiff counts the number of Mondays until date2. It counts date2 but not date1. If interval is Week ("ww"), however, the DateDiff function returns the number of calendar weeks between the two dates. It counts the number of Sundays between date1 and date2. DateDiff counts date2 if it falls on a Sunday; but it doesn't count date1, even if it does fall on a Sunday. If date1 refers to a later point in time than date2, the DateDiff function returns a negative number. The firstdayofweek argument affects calculations that use the "w" and "ww" interval symbols. If date1 or date2 is a date literal, the specified year becomes a permanent part of that date. However, if date1 or date2 is enclosed in double quotation marks (" "), and you omit the year, the current year is inserted in your code each time the date1 or date2 expression is evaluated. This makes it possible to write code that can be used in different years. When comparing December 31 to January 1 of the immediately succeeding year, DateDiff for Year ("yyyy") returns 1 even though only a day has elapsed. Examples: This example uses the DateDiff function to display the number of days between a given date and today. Dim FirstDate, Msg As String ' Declare variables. Dim SecondDate As Date FirstDate = InputBox("Enter a date") SecondDate = CDate(FirstDate) Msg = "Days from today: " & DateDiff(DateInterval.Day, Now, SecondDate) MsgBox (Msg) End: Terminates program. It's actually a statement rather than a function.
CDate converts a numeric or string expression to a Date data type. (For numeric expressions, 0 represents the date 12/30/1899. Negative values represent dates before 12/30/1899; positive values represent dates after. The decimal portion of a number represents the time for example, .5 = noon.) Numeric or string arguments outside of the range of the Date data type (1/1/100 thru 12/31/9999) results in an error. Examples: dtmTest = CDate("1/1/2000") 'dtmTest = 1/1/2000 dtmTest = CDate("January 1, 2000") 'dtmTest = 1/1/2000 dtmTest = CDate(123) 'dtmTest = 5/2/1900 dtmTest = CDate(123.456) 'dtmTest = 5/2/1900 10:56:38 AM dtmTest = CDate("ABC") 'error
CDate("May 14 1977") CDate("14/May/1977") CDate("14/05/1977") CDate("05/14/1977") CDate(0) CDate(-1) CDate(DateValue(Date)) CDate(unknown) CDate(Null) CDate("abcd") CDate(True) CDate(False)
14/05/1977 14/05/1977 14/05/1977 14/05/1977 12:00:00 AM 29/12/1899 Current System Date 12:00:00 AM Null Error 29/12/1899 12:00:00 AM Type mismatch True - Non Zero False - Zero uninitialized Variable