Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1266
  • Last Modified:

Convert Jullian Date

Does anyone have a script to covert jullian dates to calendar dates and vice versa.
0
JAYRU
Asked:
JAYRU
  • 4
  • 4
  • 4
  • +3
1 Solution
 
appariCommented:
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
 
appariCommented:
the previous one is for  converting calender date to julian date
0
 
appariCommented:
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
andyclapCommented:
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
 
CJ_SCommented:
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
 
tureCommented:
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
 
andyclapCommented:
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
 
tureCommented:
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
 
JAYRUAuthor Commented:
Thanks for the responses. Working through them now...I'll be back.
0
 
giannCommented:
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
 
andyclapCommented:
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
 
JAYRUAuthor Commented:
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
 
JAYRUAuthor Commented:
Do not understand code and no explainnation of how to use it.
0
 
tureCommented:
JAYRU,

Please don't forget to examine my short solution.

/Ture
0
 
tureCommented:
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
 
andyclapCommented:
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
 
JAYRUAuthor Commented:
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

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.

  • 4
  • 4
  • 4
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now