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
Solved

add days to a date auto

Posted on 2002-04-18
10
147 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
  • 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

856 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