Solved

add days to a date auto

Posted on 2002-04-18
10
149 Views
Last Modified: 2010-05-02
hi
I need to say have a date, how would i say add 10 days or 20 days to the date eg

my date is 15/01/2002
i want to add 20 days to it,
i need the new date

i need this functon to prompt a process after a certain time a record was captured by using the date the record was captured


thanx

Craig
0
Comment
Question by:CraigLazar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +3
10 Comments
 

Expert Comment

by:lschuele
ID: 6951178
If you have a variable that is declared as a date, it is stored as a number.  To add 10 days to a date, simply add 10 to the variable.  Hours are stored as the decimal portion of the number, so to add 12 hours, add .5 to the date variable.

Run the following code as an example.

   Dim dTheDate As Date
   
   dTheDate = #1/1/2002#
   
   Debug.Print dTheDate
   
   dTheDate = dTheDate + 20
   
   Debug.Print dTheDate
0
 
LVL 4

Author Comment

by:CraigLazar
ID: 6951199
hi ischuele
thanx for the quick respponse

this code, will it work out the correct date say from a date that is at the end of the month to the new month
say from 25 of February to March ?

thanx
0
 
LVL 7

Accepted Solution

by:
Z_Beeblebrox earned 50 total points
ID: 6951218
It is better practice to use the DateAdd method.

NewData = DateAdd("d",20,OldDate)

Zaphod.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:deighton
ID: 6951227
yes
0
 

Expert Comment

by:lschuele
ID: 6951381
Use the DateAdd function to add days, months, years, and several other options.

And Beeble is also correct that it is better practice to use the function.  dTheDate = dTheDate + 20 uses an implicit conversion from integer to date.  Implicit conversions are bad practice, as occasionaly you will get unexpected results and type mismatch errors.
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6951929
err...dateadd()...oh...someone already said that...
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6952387
lschuele,

>> And Beeble is also correct that it is better practice to use the function.  dTheDate = dTheDate + 20
uses an implicit conversion from integer to date.  Implicit conversions are bad practice, as occasionaly
you will get unexpected results and type mismatch errors. <<

Not sure if you are aware, but a date varaible is in fact a double.  The whole (integer) part is the number of days,
the fractional part is the time.  So it is no different from doing:
Dim x as Double
Dim y as Double

y = x + 20

Sure you can wrap this addition in a class and call it DoubleAdd, but would you?

Just my 2 cents worth,
Anthony
0
 
LVL 7

Expert Comment

by:Z_Beeblebrox
ID: 6952427
My 2 cents :)

From a good programming practice standpoint, there is nothing wrong with adding two doubles together, nor is there anything wrong with adding two dates together (although I can't image why anyone would want to). The problems arises when you try to add a double (or an integer in this case) to a date. It is bad practice to add variable of different types together because doing so safely requires knowledge of the underlying structure of the variables in question, something which not all readers of a given piece of code will have.

Hopefully I am not starting a whole debate on good programming, I just think that not everyone who sees code that adds 20 to a date will know what that means, whereas dateadd("d", 20, SomeDate) is very self explanatory.

Zaphod.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 6952438
Then we will have to agree to disagree.

Anthony
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6953690
you do that................hmmmm.....handbags.....
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question