Solved

Date Function

Posted on 2003-12-01
3
711 Views
Last Modified: 2010-05-03
I am trying to use the Date function to return a date range from the current date to 5 days back.  The trick is it needs to be formatted like this:
2003-11-15-2003-11-30

Below is my orginal code.  However, as I discovered this morning, it doesn't take into consideration month changes or leap years.  Is there an easy way to do this??
   
daysBack = Year(Date) & "-" & Month(Date) & "-" & Day(Date) - 5
currentDate = Year(Date) & "-" & Month(Date) & "-" & Day(Date)  
rangeDate = daysBack & "-" & currentDate
0
Comment
Question by:jennifere
  • 2
3 Comments
 
LVL 9

Expert Comment

by:dancebert
ID: 9852178
Use the DateDiff Function

0
 
LVL 9

Accepted Solution

by:
dancebert earned 125 total points
ID: 9852202
Oops, I meant the DateAdd fuction

Dim currentDate As Date
currentDate = Now
Dim daysBack As Date
daysBack = DateAdd("d", -5, currentDate)
rangeDate = cstr(daysBack) & "-" & currentDate
0
 

Author Comment

by:jennifere
ID: 9852265
That will work-  Thanks.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
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…

840 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