ACC2000: Functions for Calculating and Displaying Date/Time ValuesID: q210604
|
Because a Date/Time value is stored as a double-precision number, you may
receive incorrect formatting results when you try to manipulate Date/Time
values in an expression. This article demonstrates how to create expressions and custom functions for displaying specific dates and for calculating time intervals.
Microsoft provides programming examples for illustration only, without warranty
either expressed or implied, including, but not limited to, the implied warranties of
merchantability and/or fitness for a particular purpose. This article assumes that you
are familiar with the programming language being demonstrated and the tools used to
create and debug procedures. Microsoft support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to provide added
functionality or construct procedures to meet your specific needs. If you have limited
programming experience, you may want to contact a Microsoft Certified Solution Provider
or the Microsoft fee-based consulting line at (800) 936-5200. For more information about
Microsoft Certified Solution Providers, please see the following page on the World Wide Web:
http://www.microsoft.com/mcsp/For more information about the support options available from Microsoft, please see the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp
DateSerial(Year(Date()), Month(Date()), 1)
DateSerial(Year(Date()), Month(Date()) + 1, 1)
DateSerial(Year(Date()), Month(Date()) + 1, 0)
DateSerial(Year(Date()), Month(Date()) + 2, 0)
DateSerial(Year(Date()), Month(Date())-1,1)
DateSerial(Year(Date()), Month(Date()),0)
DateSerial(Year(Date()), Int((Month(Date()) - 1) / 3) * 3 + 1, 1)
DateSerial(Year(Date()), Int((Month(Date()) - 1) / 3) * 3 + 4, 0)
Date() - WeekDay(Date()) + 1
Date() - WeekDay(Date()) + 7
Date() - WeekDay(Date(), 0) + 1
Date() - WeekDay(Date(), 0) + 7
Q210249 ACC2000: How to Get the Fiscal Year/Month of a Particular DateCAUTION: Following the steps in this example will modify the sample database Northwind.mdb. You may want to back up the Northwind.mdb file and perform these steps on a copy of the database.
StartDate=#6/1/93 8:00AM#
EndDate=#6/3/93 1:00PM#
?Format(EndDate-StartDate,"hh:mm")
'-------------------------------------------------------------------
' This sample code separates a time interval into seven variables for
' the following values: days, hours, minutes, seconds, total time in
' hours, total time in minutes, and total time in seconds.
'
' The interval argument is flexible; it can be a single value, an
' expression, or a field reference.
'-------------------------------------------------------------------
Dim totalhours As Long, totalminutes As Long, totalseconds As Long
Dim days As Long, hours As Long, minutes As Long, seconds As Long
Dim interval As Variant
days = Int(CSng(interval))
totalhours = Int(CSng(interval * 24))
totalminutes = Int(CSng(interval * 1440))
totalseconds = Int(CSng(interval * 86400))
hours = totalhours Mod 24
minutes = totalminutes Mod 60
seconds = totalseconds Mod 60
Option Explicit
Function GetElapsedDays (interval)
Dim days As Long
days = Int(CSng(interval))
GetElapsedDays = days & " Days "
End Function
Field: ShippedDate
Show: True
Field: OrderDate
Show: True
Field: ElapsedTime: GetElapsedDays([ShippedDate]-[OrderDate])
Show: True
Table: TimeLog
-----------------------
Field Name: StartTime
Data Type: Date/Time
Format: General Date
Field Name: EndTime
Data Type: Date/Time
Format: General Date
StartTime EndTime
--------------------------------------------
5/10/95 4:57:00 PM 5/15/95 2:38:00 AM
5/11/95 10:17:31 AM 5/24/95 6:05:00 PM
5/18/95 9:16:43 AM 5/19/95 5:03:00 PM
Option Explicit
Function GetElapsedTime(interval)
Dim totalhours As Long, totalminutes As Long, totalseconds As _
Long
Dim days As Long, hours As Long, Minutes As Long, Seconds As Long
days = Int(CSng(interval))
totalhours = Int(CSng(interval * 24))
totalminutes = Int(CSng(interval * 1440))
totalseconds = Int(CSng(interval * 86400))
hours = totalhours Mod 24
Minutes = totalminutes Mod 60
Seconds = totalseconds Mod 60
GetElapsedTime = days & " Days " & hours & " Hours " & Minutes & _
" Minutes " & Seconds & " Seconds "
End Function
Textbox
---------------
Name: ElapsedTime
ControlSource: =GetElapsedTime([EndTime]-[StartTime])
Width: 3 inches
Table: TimeCard
-----------------------
Field Name: Daily Hours
Data Type: Date/Time
Format: Short Time
8:15
7:37
8:12
8:03
Option Explicit
Function GetTimeCardTotal ()
Dim db As DAO.Database, rs As DAO.Recordset
Dim totalhours As Long, totalminutes As Long
Dim days As Long, hours As Long, minutes As Long
Dim interval As Variant, j As Integer
Set db = dbengine.workspaces(0).databases(0)
Set rs = db.OpenRecordset("timecard")
interval = #12:00:00 AM#
While Not rs.EOF
interval = interval + rs![Daily hours]
rs.MoveNext
Wend
totalhours = Int(CSng(interval * 24))
totalminutes = Int(CSng(interval * 1440))
hours = totalhours Mod 24
minutes = totalminutes Mod 60
GetTimeCardTotal = totalhours & " hours and " & minutes & " minutes"
End Function
?GetTimeCardTotal()
For additional information about calculating date/time values, please see the following article(s) in the Microsoft Knowledge Base:
Q210276 ACC2000: Storing, Calculating, and Comparing Date/Time Data
Additional query words: fractional
Keywords : kbprg kbdta AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 6, 1999