Solved

add days to a date auto

Posted on 2002-04-18
10
146 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro which automatically sends attachment to Outlook 14 71
VB 6.0 printer how to align 6 62
Microsoft Access combo box help 2 47
VBA Shell can't Find Word document 11 94
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
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…
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…

810 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