Solved

# Convert Jullian Date

Posted on 2001-08-23
1,061 Views
Does anyone have a script to covert jullian dates to calendar dates and vice versa.
0
Question by:JAYRU
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 4
• 4
• 4
• +3

LVL 39

Expert Comment

ID: 6420533
this is the function written in javascript we are using in one of our web pages. try to modify this to VB function or i suppose if you have MS scripting ocx you can run this drectly in VB ( i am not sure)

function compute(form) {
MM=eval(form.month.value)
DD=eval(form.day.value)
YY=eval(form.year.value)
HR=eval(form.hour.value)
MN=eval(form.minute.value)
with (Math) {
HR = HR + (MN / 60);
GGG = 1;
if (YY <= 1585) GGG = 0;
JD = -1 * floor(7 * (floor((MM + 9) / 12) + YY) / 4);
S = 1;
if ((MM - 9)<0) S=-1;
A = abs(MM - 9);
J1 = floor(YY + S * floor(A / 7));
J1 = -1 * floor((floor(J1 / 100) + 1) * 3 / 4);
JD = JD + floor(275 * MM / 9) + DD + (GGG * J1);
JD = JD + 1721027 + 2 * GGG + 367 * YY - 0.5;
JD = JD + (HR / 24);
}
form.result.value = JD;
}

0

LVL 39

Expert Comment

ID: 6420537
the previous one is for  converting calender date to julian date
0

LVL 39

Accepted Solution

appari earned 100 total points
ID: 6420552
0

LVL 6

Expert Comment

ID: 6421072
VBS or VB?
VB has built in date functions which are reasonably good, and also dates are stored internally in julian format (since 1899-12-30)

Take a look in the help files for dateserial, format, cdate, datepart, dateadd, datediff etc.
0

LVL 22

Expert Comment

ID: 6421107
From normal to julian:

Public Function getJulianDate(ByVal dVal As Date) As Long
getJulianDate = DateDiff("d", "1/1/1900", dVal) + 2
End Function

Regards,
CJ
0

LVL 22

Expert Comment

ID: 6421274
JAYRU,

Something like this?

Sub TestIt()

MsgBox JulianToDate("2001-1")
MsgBox JulianToDate("2001-32")
MsgBox JulianToDate("2001-365")

End Sub

Function JulianToDate(j As String) As Date
'Converts Julian date YYYY-DDD to date
JulianToDate = DateSerial(Left(j, 4), 1, Mid(j, 6))
End Function

Ture Magnusson
0

LVL 6

Expert Comment

ID: 6421277
CJ_S:
Don't forget VB uses julian dates, so you can just use
clng(dt) instead of your function to convert to a long julian value (based on 31/12/1899)

I'd recommend only ever using real date variables within the application, only converting to and from text formats on input and output/display

0

LVL 22

Expert Comment

ID: 6421280
JAYRU,

If the Julian date is without the dash (YYYYDDD), use this slightly modified function instead:

Function JulianToDate(j As String) As Date
'Converts Julian date YYYYDDD to date
JulianToDate = DateSerial(Left(j, 4), 1, Mid(j, 5))
End Function

/Ture
0

Author Comment

ID: 6421696
Thanks for the responses. Working through them now...I'll be back.
0

Expert Comment

ID: 6423591
1. Conversion of Julian Date to Calendar date:

JD = JDE + 0.5  'JDE= Julian Date
Z = Int(JD)
F1 = JD - Z
If Z > 2299161 Or Z = 2299161 Then
a = Int((Z - 1867216.25) / 36524.25): A = Z + 1 + a - Int(a / 4)
Else: A = Z
End If
B = A + 1524
C = Int((B - 122.1) / 365.25)
d1 = Int(365.25 * C)
E = Int((B - d1) / 30.6001)
Da = B - d1 - Int(30.6001 * E) + F1
Da = Int(Da) 'Da= Day
If E < 14 Then M = E - 1
If E = 14 Or E = 15 Then M = E - 13  'M=Month
If M>2 Then Y=C-4716
If M=1 Or M=2 Then Y=C-4715  'Y=Year
CalDate=DateSerial(Y, M, Da)
CalDate = Format\$(CalDate, "dddd d  mmm.")

2. Conversion Calendar date to Julian Date:

'D=Day, M=Month,Y=Year
If M = 1 Or M = 2 Then
Y = Y - 1: M = M + 12
End If
A = Int(Y / 100)
B = 2 - A + Int(A / 4)
JDE = Int(365.25 * (Y + 4716)) + Int(30.6001 * (M + 1)) + D + B - 1524.5   'Julian Day
0

LVL 6

Expert Comment

ID: 6423822
giann - it's generally considered impolite here on E.E. to propose answers unless you know for sure than your answer is unambiguously the only, and correct, answer to a question.

0

Author Comment

ID: 6423927
APPARI,
The link you posted has got my attention with that code for the Julian to caldate. I had a similar function that worked like a charm and looked very much like the code in the link you posted. My problem is not being able to figure out how call that code in a access module or vb for that matter. I have a table called "julian" with a column in it called "jdate". I may have thousands of records that I need to covert.  How do I do this with the below code. I'll increase the points as this is a lot of trouble.
______________________________________
Function GetDateFromJulian(ByVal JulianVal As String) As Variant
Dim sDy As String
Dim sMth As String
Dim sYr As String
Dim dDateVal As Date

'-- Set Error Handler
On Error GoTo GetDateFromJulianERR

'-- Get Month Value from Julian Date Parm.
sMth = Format(Right\$(JulianVal, 3), "mm")
'-- Get Day Value from Julian Parm.
sDy = Format(Right\$(Val(JulianVal + 1), 3), "dd")

'-- Check Julian Val passed in
'
'-- If Length of Parm. is 5 Then Short Julian Date Passed in       ( YYDDD )
'-- ELSE
'-- If Length of Parm. is 7 then Long Julian date passed in         ( YYYYDDD )

'-- If Len = 5 then Short Julian
If Len(JulianVal) = 5 Then
'-- Strip Two Digit Year from Parm.
sYr = Left\$(JulianVal, 2)
'-- Set Date variable = to Current Gregorian format
dDateVal = CDate(sMth & "/" & sDy & "/" & sYr)
'-- Set Function = to Gregorian Date Value
GetDateFromJulian = dDateVal
Exit Function

'-- If Len = 7 then Long Julian
ElseIf Len(JulianVal) = 7 Then
'-- Strip Four Digit Year
sYr = Left\$(JulianVal, 4)
'-- Set Date variable = to Current Gregorian format
dDateVal = CDate(sMth & "/" & sDy & "/" & sYr)
'-- Set Function = to Gregorian Date Value
GetDateFromJulian = dDateVal
Exit Function

Else
GoTo GetDateFromJulianERR
End If

Exit Function
GetDateFromJulianERR:
GetDateFromJulian = Null

End Function
______________________
0

Author Comment

ID: 6423933
Do not understand code and no explainnation of how to use it.
0

LVL 22

Expert Comment

ID: 6424213
JAYRU,

Please don't forget to examine my short solution.

/Ture
0

LVL 22

Expert Comment

ID: 6424223
JAYRU,

Here is how to call my function from a VB procedure:

sub TestConversion()

dim d as date
dim jd as string

jd = "2001123"
d = JulianToDate(jd)

end sub

/Ture
0

LVL 6

Expert Comment

ID: 6424866
A quick question - how is the julian date stored? Is it a string in YYYYDDD format, or a numeric type, or is it a value showing the number of days since a particular point in time, such as 1st Jan 1900.
This will help us out giving you the best answer.
Incidentally, as you're using a database it should be pretty easy to write a snippet of code which you can use directly from your select statements - am I right in presuming you're using Access?
0

Author Comment

ID: 6425278
APPARI,
Your the first to respond and I ended up using your method and it worked great. Thanks to all the other contributors...JayRU
0

## Featured Post

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â€¦
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we aâ€¦
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â€¦
###### Suggested Courses
Course of the Month7 days, 21 hours left to enroll