Solved

Convert Jullian Date

Posted on 2001-08-23
17
945 Views
Last Modified: 2010-08-05
Does anyone have a script to covert jullian dates to calendar dates and vice versa.
0
Comment
Question by:JAYRU
  • 4
  • 4
  • 4
  • +3
17 Comments
 
LVL 39

Expert Comment

by:appari
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

by:appari
ID: 6420537
the previous one is for  converting calender date to julian date
0
 
LVL 39

Accepted Solution

by:
appari earned 100 total points
ID: 6420552
0
 
LVL 6

Expert Comment

by:andyclap
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

by:CJ_S
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

by:ture
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
Karlstad, Sweden
0
 
LVL 6

Expert Comment

by:andyclap
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

by:ture
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
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:JAYRU
ID: 6421696
Thanks for the responses. Working through them now...I'll be back.
0
 

Expert Comment

by:giann
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

by:andyclap
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

by:JAYRU
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
        '-- Error Out Set RETURN to NULL
        GoTo GetDateFromJulianERR
    End If

Exit Function
GetDateFromJulianERR:
    GetDateFromJulian = Null

End Function
______________________
0
 

Author Comment

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

Expert Comment

by:ture
ID: 6424213
JAYRU,

Please don't forget to examine my short solution.

/Ture
0
 
LVL 22

Expert Comment

by:ture
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

by:andyclap
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

by:JAYRU
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

746 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

18 Experts available now in Live!

Get 1:1 Help Now