[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

Adding seconds to a date value

Posted on 2000-04-21
Medium Priority
184 Views
Last Modified: 2010-05-02
Dim StartDate as date
StartDate=Now

how can i add 1 second to StartDate?
0
Question by:hess
[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
• 4
• 3
• 2
9 Comments

LVL 12

Accepted Solution

mark2150 earned 40 total points
ID: 2738442
DateAdd

From the help:

Returns a Variant (Date) containing a date to which a specified time interval has been added.

Syntax

DateAdd(interval, number, date)

The DateAdd function syntax has these named arguments:

Part      Description
interval      Required. String expression that is the interval of time you want to add.
number      Required. Numeric expression that is the number of intervals you want to add. It can be positive (to get dates in the future) or negative (to get dates in the past).
date      Required. Variant (Date) or literal representing date to which the interval is added.
Settings

The interval argument has these settings:

Setting      Description
yyyy      Year
q      Quarter
m      Month
y      Day of year
d      Day
w      Weekday
ww      Week
h      Hour
n      Minute
s      Second
Remarks

You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now.
To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").
The DateAdd function won't return an invalid date. The following example adds one month to January 31:

DateAdd("m", 1, "31-Jan-95")

In this case, DateAdd returns 28-Feb-95, not 31-Feb-95. If date is 31-Jan-96, it returns 29-Feb-96 because 1996 is a leap year.

If the calculated date would precede the year 100 (that is, you subtract more years than are in date), an error occurs.

If number isn't a Long value, it is rounded to the nearest whole number before being evaluated.
0

Author Comment

ID: 2738447
dim sDate as date
MsgBox sDate & "   :   " & sDate + 0.00001

that works... will it always?... i was just trying things out
0

LVL 12

Expert Comment

ID: 2738469
Internally dates are stored as a double. The integer part is the date and the decimal portion is the time. Adding an appropriately small value will work - sort of. If you want accurate time computations, use the functions provided for that purpose.

M
0

LVL 9

Expert Comment

ID: 2738474
Option Explicit

Private Sub Form_Load()

MsgBox ("Now + 1 seconds: " & DateAdd("s", 1, Now) & ("Now: " & DatePart("s", Now)))

End Sub
0

Author Comment

ID: 2738498
Here is what happens. I have a timer that fires every second. ... Just to keep track of time. And i use datediff to see howmany seconds have passed. I get the diff between now and a stored date. I want to be able to pause the time keeping. I have a date pausetime that is the pasue time and i thought i could add the difference between pausetime and now to starttime to move start time along with the real time to make it appear that the clock was paused..i used this code..

StartTime = DateAdd("s", DateDiff("s", PauseTime, Now), StartTime)

.... but it doesn't work...
0

LVL 9

Expert Comment

ID: 2738503
Use DataAdd function to add to a date or time.

Use DataPart function to get the part of the date or time...

Private Sub Form_Load()

MsgBox ("Now + 1 seconds: " & DateAdd("s", 1, Now) & vbTab & ("Second Now : " & DatePart("s", Now)))
End Sub
0

Author Comment

ID: 2738519
DateDiff("s", PauseTime, Now)... this part is right..i can watch it incriment in seconds....
0

LVL 12

Expert Comment

ID: 2738530
You want to measure elapsed time or real time? Elapsed time is simple, make a global or a static var in your timer routine and shut it off and back on. Real time is same-same except you display NOW instead of doing date computations.

M
0

Author Comment

ID: 2738547
that is just what i needed. I was adding 1 second after the first second and then 2 seconds after the 2nd second... instead of 1 each time. Thanks for the help.
0

Featured Post

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

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

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