Solved

time problem

Posted on 2002-05-27
11
155 Views
Last Modified: 2010-05-02
Doesn anybody know how to calculate a time difference,

when i write some code to get the result of say (9pm - 8am) it keeps adding 12 hours on.

but if i do it from 1am - 6am then it works the time out correct.

can somebody show me some code that gives me the hours difference from 8pm - 6am.

cheers
Andy
0
Comment
Question by:andysalih
  • 4
  • 4
  • 2
  • +1
11 Comments
 
LVL 8

Expert Comment

by:glass_cookie
ID: 7036617
Hi!

You could use the datediff function to do that.  Here's the extract from my help file (use 'h'):


Returns a Variant (Long) specifying the number of time intervals between two specified dates.

Syntax

DateDiff(interval, date1, date2[, firstdayofweek[, firstweekofyear]])

The DateDiff function syntax has these named arguments:

Part     Description
interval     Required. String expression that is the interval of time you use to calculate the difference between date1 and date2.
date1, date2     Required; Variant (Date). Two dates you want to use in the calculation.
firstdayofweek     Optional. A constant that specifies the first day of the week. If not specified, Sunday is assumed.
firstweekofyear     Optional. A constant that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.
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

That's it!

glass cookie : )
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7036626
hi andysalih,

Try this,
X = Abs(DateDiff("H", "21:00:00", "8:00:00"))

where the time is in 24hr format

hope this helps!

-priya
0
 
LVL 8

Expert Comment

by:glass_cookie
ID: 7036628
Oops - now I get what you mean.  Since the datediff function also gives you the same problem, how about reversing the time?  As in...

Instead of:

MsgBox DateDiff("h", "8:00 PM", "6:00 AM")

You do something like:

MsgBox DateDiff("h", "6:00 PM", "8:00 AM")

Of course, you'll need some coding to determine is the switching is required by using the instr function to check for 'AM' or 'PM'

That's it!

glass cookie ; )
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:priya_pbk
ID: 7036629

therefore X would return "13"

0
 
LVL 1

Expert Comment

by:LJunior
ID: 7036907
Hello,

if the two 'times' are not within the same day you've got to use the date to tell the DateDiff function what to do

DateDiff("H", "01/01/2002 21:00:00", "01/02/2002 8:00:00")
0
 
LVL 3

Author Comment

by:andysalih
ID: 7036999
i dont understand the calcs here

in my world 9pm - 8am in the morning is 11 hours, How can it be 13.

why is it returning the value 13


cheers

Andy
0
 
LVL 2

Accepted Solution

by:
priya_pbk earned 50 total points
ID: 7037024
oops!! sorry abt that, happens in my world too!


MsgBox DateDiff("H", Format("27/07/2002 21:00:00", "dd-mm-yyyy hh:mm:ss"), Format("28/07/2002 08:00:00", "dd-mm-yyyy hh:mm:ss"))



0
 
LVL 3

Author Comment

by:andysalih
ID: 7037055
i dont understand the calcs here

in my world 9pm - 8am in the morning is 11 hours, How can it be 13.

why is it returning the value 13


cheers

Andy
0
 
LVL 3

Author Comment

by:andysalih
ID: 7037059
oops. REFRESHED by mistake sorry guys
0
 
LVL 2

Expert Comment

by:priya_pbk
ID: 7037076

'Formatted:

Private Sub Command1_click()

MsgBox DateDiff("H", Format("27/07/2002 21:00:00", "dd-mm-yyyy hh:mm:ss"), Format("28/07/2002 08:00:00",
"dd-mm-yyyy hh:mm:ss"))

'the above returns 11

MsgBox DateDiff("H", Format("27/07/2002 20:00:00", "dd-mm-yyyy hh:mm:ss"), Format("28/07/2002 06:00:00", "dd-mm-yyyy hh:mm:ss"))

'this returns 10

End Sub


-priya

0
 
LVL 3

Author Comment

by:andysalih
ID: 7037092
thats the one,

cheers
Andy
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows 10 start screen issues 9 55
IF ELSE Statement in Excel Macro VBA 16 75
how to Classify of email (pdf attachments) 7 45
Dinamic report to Crosstab query 9 33
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…

860 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