ACC2000: How to Convert Julian Days to Dates in Access and Back
ID: Q209922
|
The information in this article applies to:
Moderate: Requires basic macro, coding, and interoperability skills.
SUMMARY
This article shows you how to create a sample procedure that converts
Julian days to dates in Microsoft Access, and a sample procedure that
converts Microsoft Access dates to Julian days. You can change the Julian
day format used in the examples to a format tailored to your specific
needs. Examples of conversion methods follow each sample procedure.
MORE INFORMATION
There are many definitions of Julian days and Julian dates. The formal
definition of a Julian date is not used in this article because of the
complex historical adoption of the Gregorian (modern) calendar. Instead, an
informal Julian day format is used. It is defined as the ordinal day of a
year.
The informal Julian day is commonly used by government agencies and
contractors. The informal Julian day format used in this article is the
ordinal day of a year (for example, Julian day 032 represents February
1st, or the 32nd day of the year). Variations on informal Julian day
formats include using a preceding two-digit year (for example 99032 for
2/1/1999) and separating the year with a dash (for example 99-032). Another, less popular, Julian day format uses a one digit year (for example 9-032). These additional formats do not uniquely identify the century or decade. You should carefully consider the consequences when using these formats; for example, the Julian day 00061 can be interpreted as 3/1/2000 or 3/2/1900.
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
The first sample function, CJulian2Date(), converts a Julian day to a date:
- Create a module and type the following line in the Declarations section if it is not already there:
Option Explicit
- Type the following procedure:
' *********************************************************************
' FUNCTION: CJulian2Date()
'
' PURPOSE: Convert a Julian day to a date. The function works with
' dates based on the Gregorian (modern) calendar.
'
' ARGUMENTS:
' JulDay: The ordinal day of a year. Between 1 and 365 for all
' years, or between 1 and 366 for leap years.
'
' YYYY: A three or four digit integer for a year that is within the
' range of valid Microsoft Access dates. If YYYY is omitted,
' then YYYY is assumed to be the year of the current system
' date.
'
' RETURNS: A date for a valid Microsoft Access year and Julian day,
' or a Null value for an invalid Julian Day.
' *********************************************************************
Function CJulian2Date (JulDay As Integer, Optional YYYY)
If IsMissing(YYYY) Then YYYY = Year(Date)
If Not IsNumeric(YYYY) Or YYYY \ 1 <> YYYY Or YYYY < 100 Or YYYY _
> 9999 Then Exit Function
If JulDay > 0 And JulDay < 366 Or JulDay = 366 And _
YYYY Mod 4 = 0 And YYYY Mod 100 <> 0 Or YYYY Mod 400 = 0 Then _
CJulian2Date = Format(DateSerial(YYYY, 1, JulDay), "m/d/yyyy")
End Function
To test this function, follow these steps:
- Type the following line in the Immediate window and then press ENTER:
?CJulian2Date(32, 1999)
Note that your system's date format for 2/1/1999 is returned. If your system's date is in 1999 then CJulian2Date(32) will also return 2/1/1999.
- To convert a Julian date format like 99032 to a Microsoft Access date, type the following line in the Immediate window, and then press ENTER:
?CJulian2Date(99032 Mod 1000, 1900 + 99032 \ 1000)
Note that your system's date format for 2/1/1999 is returned. Replace 1900 with 2000 if you are using this Julian date format for dates between 1/1/2000 and 12/31/2999.
- To convert a Julian date format like 99-032 to a Microsoft Access date, type the following line in the Immediate window, and then press ENTER:
?CJulian2Date(Right("99-032", 3), 1900 + Left("99-032", 2))
Note that your system's date format for 2/1/1999 is returned. Replace 1900 with 2000 if you are using this Julian date format for dates between 1/1/2000 and 12/31/2999.
The second sample function, CDate2Julian(), converts a Microsoft Access date to a Julian day:
- Create a module and type the following line in the Declarations section if it is not already there:
Option Explicit
- Type the following procedure:
' *********************************************************************
' FUNCTION: CDate2Julian()
'
' PURPOSE: Convert a date to a Julian day. The function works with
' dates based on the Gregorian (modern) calendar.
'
' ARGUMENTS:
' MyDate: A valid Microsoft Access date.
'
' RETURNS: A three digit Julian day as a string.
' *********************************************************************
Function CDate2Julian(MyDate As Date) As String
CDate2Julian = Format(MyDate - DateSerial(Year(MyDate) - 1, 12, _
31), "000")
End Function
- To test this function, type the following line in the Immediatewindow, and then press ENTER:
?CDate2Julian(#3/1/1999#)
Note that Julian day 060 is returned.
- To convert a valid Microsoft Access date to a Julian day in a format like 99060, type the following line in the Immediate window, and then press ENTER:
?Year(#3/1/1999#) Mod 100 & CDate2Julian(#3/1/1999#)
Note that 99060 is returned.
- To convert a valid Microsoft Access date to a Julian day in a format like 99-060, type the following line in the Immediate window, and then press ENTER:
?Year(#3/1/1999#) Mod 100 & "-" & CDate2Julian(#3/1/1999#)
Note that 99-060 is returned.
Additional query words:
INF
Keywords : kbprg kbdta kb2000 AccCon KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
|