【问题标题】:How to convert Julian date ignoring the year to Julian Day如何将忽略年份的儒略日期转换为儒略日
【发布时间】:2018-07-30 10:13:25
【问题描述】:

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

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

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

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

如上例,我需要获取值 032

【问题讨论】:

  • 没有年份,你将如何处理闰年?其他语言是指英语以外的语言或其他编程语言吗?如果您可以输入年份,DateTime.DayOfYear 可能会成功。
  • 一年中月份的天数与公历相同,但闰年略有不同,因为它没有闰年的 100 年例外。因此,正如@Mary 所说,您仍然需要包括年份。
  • 我的意思是给定年份、月份和日期,儒略日是基于我给出的链接,只有 3 位数的值,我该怎么做,正常的在线儒略转换器是基于日期的转换后的值超过 3 位。其他语言是指任何其他编程语言,我只需要研究转换的编程逻辑。任何编程语言都可以,但最好是 vb6 我正在研究的环境
  • 为了澄清您的问题,对于日期为 1900 年 3 月 1 日,结果应该是 60 还是 61?

标签: vba vb.net julian-date


【解决方案1】:

.Net JulianCalendar Class 公开了实现目标所需的所有方法。它也是 COM 可见的,因此您可以在添加对“mscorlib.dll”的项目引用后在 VBA 项目中引用它。

在 VB.Net 中,代码为:

Dim jc As New System.Globalization.JulianCalendar
' create a gregorian equivalent of the julian date: 1 Feb 2018
Dim gregoriaDateEquivalent As DateTime = jc.ToDateTime(2018, 2, 1, 0, 0, 0, 0)
' = #2/14/2018 12:00:00 AM#
Dim dayOfYear As Int32 = jc.GetDayOfYear(gregoriaDateEquivalent)
' = 32

在 VBA 中,代码为:

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, 2, 1, 0, 0, 0, 0)
' = #2/14/2018
Dim dayOfYear As Long
dayOfYear = jc.GetDayOfYear(gregoriaDateEquivalent)
' = 32

【讨论】:

  • @ThomasKoh,如果它“有效”,请考虑将其标记为答案。
【解决方案2】:

首先你必须添加对 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 位数

【讨论】:

    猜你喜欢
    • 2013-07-30
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 2015-11-08
    相关资源
    最近更新 更多