Datediff () function for calculating time difference in VB ()

Source: Internet
Author: User
Datediff Function

ReturnVariant(Long) Indicates the number of time intervals between two specified days.

Syntax

Datediff (interval, date1, date2 [, firstdayofweek [, firstweekofyear])


Part Description
Interval Required. String expression used to calculateDate1AndDate2Time Difference
Date1 □date2 Required;Variant(Date). The two dates used in the calculation.
Firstdayofweek Optional. Specifies the constant of the first day of a week. If not specified, Sunday is the first day.
Firstweekofyear Optional. Specifies the constant of the first week of a year. If this parameter is not specified, the week that contains April 1 is the first week.

Set

The interval parameter settings are as follows:

Set Description
Yyyy Year
Q Quarter
M Month
Y Number of days per year
D Day
W Number of days in a week
WW Week
H Hour
N Minutes
S Seconds

For calculationDate1AndDate2The number of days in a different year can be "the number of days in a year" (y) or "day" (d ). When
IntervalWhen "Number of days of a week" (W), datediff returns the number of weeks in the two-day period. IfDate1It is Monday, and datediff calculatesDate2The number of Monday ends. This number containsDate2But does not containDate1. However, ifInterval
If the value is "Week" (WW), The datediff function returns the number of "calendar weeks" in the two-day period. By computingDate1AndDate2
The number of Sundays varies. IfDate2It's just Sunday, then
Date2It will also be added to the datediff count result.Date1If it is Sunday or not, it is not included.

IfDate1RatioDate2
If it comes late, the return value of the datediff function is negative.

IfDate1OrDate2Is the date text, the specified year becomes a fixed part of the date. However, ifDate1Or
Date2Enclose it with double quotation marks (""), and the year is omitted.Date1Or
Date2The current year is inserted into the code. In this way, you can write the program code for different years.

 

The following code is used as an example:

Dim startdate as date
Dim starttime as date
Dim enddate as date
Dim endtime as date

Dim costdate as long
Dim costtime as long

 

Startdate = format (TRIM (MRC. Fields (2), "Mm-dd hh: mm ")
Starttime = format (TRIM (MRC. Fields (3), "Mm-dd hh: mm ")
Enddate = format (TRIM (MRC. Fields (4), "Mm-dd hh: mm ")
Endtime = format (TRIM (MRC. Fields (5), "Mm-dd hh: mm ")

Costdate = datediff ("N", trim (startdate), trim (enddate ))
Costtime = datediff ("N", trim (starttime), trim (endtime ))

Txtcosttime = format (STR (costtime/60), "0.00 ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.