Solved

add days to a date auto

Posted on 2002-04-18
10
144 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
Comment Utility
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
Comment Utility
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
Comment Utility
It is better practice to use the DateAdd method.

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

Zaphod.
0
 
LVL 18

Expert Comment

by:deighton
Comment Utility
yes
0
 

Expert Comment

by:lschuele
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 4

Expert Comment

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

Expert Comment

by:Anthony Perkins
Comment Utility
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
Comment Utility
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
Comment Utility
Then we will have to agree to disagree.

Anthony
0
 
LVL 4

Expert Comment

by:nutwiss
Comment Utility
you do that................hmmmm.....handbags.....
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now