Solved

Adding seconds to a date value

Posted on 2000-04-21
9
169 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

770 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