Solved

Adding seconds to a date value

Posted on 2000-04-21
9
163 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

760 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

22 Experts available now in Live!

Get 1:1 Help Now