首页 > 解决方案 > 如何将忽略年份的儒略日期转换为儒略日

问题描述

如何在 Visual Basic 中将日/月转换为儒略日?其他语言的转换公式也值得赞赏。

例如,2 月 1 日的儒略日 = 032

https://landweb.modaps.eosdis.nasa.gov/browse/calendar.html

根据网络研究,大多数解决方案都将带有 yyyy-mm-dd 的日期转换为朱利安日期。

如上例,我需要获取值 032

标签: vbavb.netjulian-date

解决方案


首先,您必须添加对 mscorlib 库的引用在 VB6 下,Project-References->勾选 mscorlib 复选框

在您拥有的对象中创建此函数方法或以其他方式创建子过程

Public Function JulianConverter() As String
        'MsgBox Fix(5.2), vbExclamation, App.Title
        'MsgBox Fix(5.6)
        Dim y As String
        Dim m As String
        Dim d As String
        Dim strDate As String
        Dim dateArray() As String


        strDate = Format(Now, "dd/MMM/yyyy")
        dateArray = Split(strDate, "/")
        d = dateArray(0)
        m = dateArray(1)
        y = dateArray(2)
        'Debug
        'MsgBox strDate

        'Convert to Integer
        y = Val(y)
        m = Val(m)
        d = Val(d)

        If m <= 2 Then
            y = y - 1
            m = m + 12
        End If

        'Dim A As Double
       ' Dim B As Double
        'Dim JD As Double

       ' A = CDbl(Fix(y / 100#))

        'B = 2 - A + Fix(A / 4)


        'JD = Fix(365.25 * (y + 4716)) + Fix(30.6001 * (m + 1)) + d + B - 1524.5
        'JulianConverter = CStr(JD) 'Convert to string

        Dim jc As mscorlib.JulianCalendar
Set jc = New mscorlib.JulianCalendar

' create a gregorian equivalent of the julian date: 1 Feb 2018
Dim gregoriaDateEquivalent As Date
gregoriaDateEquivalent = jc.ToDateTime(2018, m, d, 0, 0, 0, 0)
' = #2/14/2018
Dim dayOfYear As Long
Dim dayOfYearS As String
Dim digitLength As Integer
Dim Counter As Integer

dayOfYear = jc.GetDayOfYear(gregoriaDateEquivalent)

'Have to ensure 3 digits values
dayOfYearS = CStr(dayOfYear)

'Count number of Digits of string
digitLength = Len(dayOfYearS)

MsgBox "DigitLength" & digitLength, vbExclamation, App.Title

'If Digits length smaller than 3,add one 0 in front
If (digitLength < 3) Then
dayOfYearS = "0" & dayOfYearS
End If

JulianConverter = dayOfYearS

End Function

这将确保儒略日的值基于当前系统日期为 3 位数


推荐阅读