Solved

Adding seconds to a date value

Posted on 2000-04-21
9
180 Views
Last Modified: 2010-05-02
Dim StartDate as date
StartDate=Now

how can i add 1 second to StartDate?
0
Comment
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

by:
mark2150 earned 10 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

by:hess
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

by:mark2150
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 9

Expert Comment

by:Ruchi
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

by:hess
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

by:Ruchi
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

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

Expert Comment

by:mark2150
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

by:hess
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
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…
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…

729 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