Page 1 of 1

Julian Date

Posted: 07.06.2003, 22:25
by ElPelado
how i convert julian date to "normal dat" and the oposite?

Posted: 07.06.2003, 23:04
by Commander David
Hi ElPelado,

i`ve asked this question for a long time and i got this Link as answer:
http://aa.usno.navy.mil/data/docs/JulianDate.html

hope it helps you.

Posted: 07.06.2003, 23:05
by jrobert
Here's a way to do it. It's written in the form of a Visual Basic function so it should be pretty easy to follow.

Code: Select all

Function JulianToDate(ByVal plJulian As Long) As Date
    Dim sJulian As String
    Dim bCentury As Byte
    Dim iYear As Integer
    Dim iDay As Integer
    ' Pad with leading zeros
    sJulian = String(6 - Len(CStr(plJulian)), "0") + CStr(plJulian)
    ' Breakdown...
    bCentury = CByte(Left(sJulian, 1))
    iYear = (1900 + (bCentury * 100) + CByte(Mid(sJulian, 2, 2)))
    iDay = CInt(Mid(sJulian, 4, 3))
    ' Return the gregorian date
    JulianToDate = DateAdd("d", iDay - 1, DateSerial(iYear, 1, 1))
End Function

Posted: 08.06.2003, 08:15
by ElPelado
thank you both!
jrobert: can you explain me where to put that code? i used the visual basic, but i forgot...

Posted: 08.06.2003, 17:57
by jrobert
Rather than trying to explain it (which is something I'm not very good at when it comes to code :oops: ), I'll paste the contents of the form1.frm file here... All you need to do is copy/paste it into a blank text file and rename it to form1.frm. :wink:

Code: Select all

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   1545
   ClientLeft      =   1620
   ClientTop       =   1755
   ClientWidth     =   2610
   LinkTopic       =   "Form1"
   ScaleHeight     =   1545
   ScaleWidth      =   2610
   Begin VB.CommandButton Command1
      Caption         =   "Get Date"
      Default         =   -1  'True
      Height          =   375
      Left            =   120
      TabIndex        =   2
      Top             =   1080
      Width           =   2295
   End
   Begin VB.TextBox Text1
      Height          =   375
      Left            =   120
      TabIndex        =   1
      Text            =   "<Enter Julian Number Here>"
      Top             =   600
      Width           =   2235
   End
   Begin VB.Label Label1
      Height          =   375
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   2295
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Function JulianToDate(ByVal plJulian As Long) As Date
    Dim sJulian As String
    Dim bCentury As Byte
    Dim iYear As Integer
    Dim iDay As Integer
    ' Pad with leading zeros
    sJulian = String(6 - Len(CStr(plJulian)), "0") + CStr(plJulian)
    ' Breakdown...
    bCentury = CByte(Left(sJulian, 1))
    iYear = (1900 + (bCentury * 100) + CByte(Mid(sJulian, 2, 2)))
    iDay = CInt(Mid(sJulian, 4, 3))
    ' Return the gregorian date
    JulianToDate = DateAdd("d", iDay - 1, DateSerial(iYear, 1, 1))
End Function

Private Sub Command1_Click()
    If IsNumeric(Text1) And Len(Text1) <= 6 Then
        Label1 = JulianToDate(Text1)
    End If
    Text1.SetFocus
    Text1_GotFocus
End Sub

Private Sub Text1_GotFocus()
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1)
End Sub

Private Sub Text1_Validate(cancel As Boolean)
    If Not (IsNumeric(Text1) And Len(Text1) <= 6) Then
        cancel = True
        Text1.SetFocus
    End If
End Sub