Understanding VB6's DateAdd function

Level:
Level2

The DateAdd vb6 function enables you to add a specified time interval to a date.

 

Syntax:

DateAdd(interval, number, date)

 

The DateAdd function syntax has these parts:

 

Part

Description

interval

Required. String expression that is the interval of time that you want to add.

 

The string expression can be any of the following:

 

Expression Description

"yyyy" Year

"q" Quarter

"m" Month

"y" Day of year

"d" Day

"w" Weekday

"ww" Week

"h" Hour

"n" Minute

"s" Second

 

Note: To add days to date, you can use either "d", "y", or "w". Be advised that the term "weekday" ("w") has nothing to do with skipping weekend days; i.e., it is NOT equivalent to "business days". Applications that would require skipping weekend days (like Saturday and/or Sunday) would have to be custom coded.

 

number

Required. Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).

 

date

Required. Date value that you want to evaluate.

 

 


To demonstrate DateAdd, set up a "Try It" project, and place the following code in the cmdTryIt_Click event:

 

Private Sub cmdTryIt_Click()

Print "Current date/time is: "; _

Format$(Now, "Long Date"); _

Spc(1); _

Format$(Now, "Long Time")

Print

Print "*** DateAdd Function Examples (with 3 as the number argument) ***"

Print "Using 'yyyy':"; Tab(20); DateAdd("yyyy", 3, Now)

Print "Using 'q':"; Tab(20); DateAdd("q", 3, Now)

Print "Using 'm':"; Tab(20); DateAdd("m", 3, Now)

Print "Using 'y':"; Tab(20); DateAdd("y", 3, Now)

Print "Using 'd':"; Tab(20); DateAdd("d", 3, Now)

Print "Using 'w':"; Tab(20); DateAdd("w", 3, Now)

Print "Using 'ww':"; Tab(20); DateAdd("ww", 3, Now)

Print "Using 'h':"; Tab(20); DateAdd("h", 3, Now)

Print "Using 'n':"; Tab(20); DateAdd("n", 3, Now)

Print "Using 's':"; Tab(20); DateAdd("s", 3, Now)

Print

Print "*** DateAdd Function Examples (with -3 as the number argument) ***"

Print "Using 'yyyy':"; Tab(20); DateAdd("yyyy", -3, Now)

Print "Using 'q':"; Tab(20); DateAdd("q", -3, Now)

Print "Using 'm':"; Tab(20); DateAdd("m", -3, Now)

Print "Using 'y':"; Tab(20); DateAdd("y", -3, Now)

Print "Using 'd':"; Tab(20); DateAdd("d", -3, Now)

Print "Using 'w':"; Tab(20); DateAdd("w", -3, Now)

Print "Using 'ww':"; Tab(20); DateAdd("ww", -3, Now)

Print "Using 'h':"; Tab(20); DateAdd("h", -3, Now)

Print "Using 'n':"; Tab(20); DateAdd("n", -3, Now)

Print "Using 's':"; Tab(20); DateAdd("s", -3, Now)

 

End Sub

 

For this example, you will need to adjust the size of your form to make it long enough to display all of the output lines.

 

Run the project and click the "Try It" button. The output should look similar to the following:

 

 

 

Download the VB project code for the example above here.


 

Adding a Time to a Date

We saw previously that you can use the DateValue and TimeValue functions to extract the date portion or time portion, respectively, from a Date field that contains both Date and Time data. What if you need to do the reverse – where you have two separate date fields, one with date data only and one with time data only, and you need to put them together in one date field that contains both date and time data? Although there is not a specific function to do this, it can be accomplished with the following code (presented in the context of a "Try It" example):

 

Private Sub cmdTryIt_Click()

Dim dtmDateOnly As Date

Dim dtmTimeOnly As Date

Dim dtmBoth As Date

 

Print "Current Date/Time is: "; Now

 

dtmDateOnly = DateValue(Now) ' isolate date portion

dtmTimeOnly = TimeValue(Now) ' isolate time portion

Print "dtmDateOnly = "; Format$(dtmDateOnly, "m/d/yyyy h:nn:ss AM/PM")

Print "dtmTimeOnly = "; Format$(dtmTimeOnly, "m/d/yyyy h:nn:ss AM/PM")

 

dtmBoth = dtmDateOnly ' initialize the combined var with the date only var

Print "dtmBoth = "; Format$(dtmBoth, "m/d/yyyy h:nn:ss AM/PM")

dtmBoth = DateAdd("h", Hour(dtmTimeOnly), dtmBoth) ' add the hour to the combined var

Print "dtmBoth = "; Format$(dtmBoth, "m/d/yyyy h:nn:ss AM/PM")

dtmBoth = DateAdd("n", Minute(dtmTimeOnly), dtmBoth) ' add the minute to the combined var

Print "dtmBoth = "; Format$(dtmBoth, "m/d/yyyy h:nn:ss AM/PM")

dtmBoth = DateAdd("s", Second(dtmTimeOnly), dtmBoth) ' add the seconds to the combined var

Print "dtmBoth = "; Format$(dtmBoth, "m/d/yyyy h:nn:ss AM/PM")

 

End Sub

 

Run the project and click the "Try It" button. The output should look similar to the following:

 

 

Download the VB project code for the example above here.

Written By TheVBProgramer.

If you enjoyed this post, subscribe for updates (it's free)

red bottom shoes

It's the highest point in the region and also is accessible using the marked trails Any Global michael kors business has the requirement for an international courier company Mr It will also be best if the previous data and reports will michael kors outlet be utilized as references But do you really know if this is the proper business for you?Many instances individuals get began in louis vuitton outlet MLMs or network marketing because of a buddy or family member's recommendation Maximum growth of the magnet industry is expected in countries Louis Vuitton Sale like India, China, Russia, Taiwan, Indonesia and Mexico, basically in the developing countries The retro glamour is my favorite, this is a michael kors outlet halfup headband style, but is indeed in the height of fashion quite considerably trendy, a guaranteed classic You just made money Start gucci bags using a personal workspace anytime possible prevent distractions While you choose the black prom dress, strapless prom dress or any other prom louis vuitton purses gowns, be certain you keep budget in mind Conversely, if the area is destined to become heated, the effect can be intensified louis vuitton outlet by placing the intensifier equipment on the opposite side of the flange so that the coil is between the flange and the louis vuitton store hub So, what is Mass Money Makers?It's based on a simple concept that works and works well If you elect to assemble michael kors outlet a wood deck you'll find the task less of a challenge as compared to working with masonryYour average internet user does not louis vuitton wonder about what makes Google tick, but anyone trying to be successful with an internet marketing campaign needs to be as informed gucci about it as possible Missing even one could cause the court to dismiss your case These apps allow consumers make their personal louis vuitton bags entries into an interactive webpage application that other people can share with It is with the help of a loan amortization calculator louis vuitton outlet that you will be able to learn how much money you will have to pay towards your monthly loan amortization With instant red bottom shoes followup mails, you are building a relationship of trust with new prospects plus maintain existing relationships with customerslarge power rates! Hovering at louis vuitton about Twenty five mere cents michael kors bags for each kw and planning to head out possibly higher, is now the right time frame for consumers to start considering positive aspects photovoltaic has to offer But if budget and work enthusiasm are the concerns then go for the new maids Christian Louboutin Outlet But obtaining a standard checkup could catch the condition and get it fixed and keep your pearly whites safe

Thank You!!

I searched for over 4 hours all over the web to get instructions how to add a time to a date. It's very hard to find help for VB6 without getting instructions for newer VB versions. Your instructions are extremely helpfull and filled my need exactly. Thank you very much!

Thanks for this - helped me

Thanks for this - helped me out.
One thing though, if you're just working in days, you don't need it. e.g. DimDate = NOW + x

Thanks for this very powerful

Thanks for this very powerful information.

adding only month to a date got from date picker

Hello
i wanted to how to add only month to a date which is got from a date picker tool please let me know the code

Thanks

Thanks for the post I needed it.

tnx for these post. i've

tnx for these post. i've been looking for these for how many days, atlast!....it helps me in finishing my navigation calculator....tnx. more power!!!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. The supported tag styles are: <foo>, [foo].

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.