在 JDE Julian Date 和 Gregorian 之间来回转换的 VBA 代码示例:
Public Const Epoch = 1900
Public Const JDateMultiplier = 1000
Public Const FirstJan = "01/01/"
Public Function Julian2Date(ByVal vDate As Long) As Date
Dim Year As Long
Dim Days As Long
Dim SeedDate As Date
' Day Number
Days = vDate - (Int(vDate / JDateMultiplier) * JDateMultiplier) - 1
' Calendar Year
Year = ((vDate - Days) / JDateMultiplier) + Epoch
' First Day of Calendar Year
SeedDate = CDate(FirstJan + CStr(Year))
' Add Number of Days to First Day in Calendar Year
Julian2Date = DateAdd("d", Days, SeedDate)
End Function
Public Function Date2Julian(ByVal vDate As Date) As Long
Dim JYear As String
Dim BeginDate As Date
Dim JDays As Long
' Calendar Year
JYear = Format(Year(vDate), "0000")
' First Day of Calendar Year
BeginDate = CDate(FirstJan + JYear)
' Day Number
JDays = DateDiff("d", BeginDate, vDate) + 1
' Add Number of Days to Year Number
Date2Julian = ((CLng(JYear) - Epoch) * JDateMultiplier) + JDays
End Function
我试图让它尽可能清晰和简单,为此我故意省略了任何错误陷阱。但是,您应该能够将代码添加到 VBA 模块并直接从您自己的代码中调用它们。
我还包括一些有用的 T-SQL 片段:
今天的日期为 JDE 朱利安日期:
(datepart(yy,getdate())-1900) * 1000 + datepart(dy, getdate())
将 JDE 儒略日期转换为公历 (DD/MM/YYYY),将 XXXXXX 替换为包含 JDE 儒略日期的列名:
convert (varchar, dateadd (day,convert (int, right(XXXXXX,3)) - 1, convert (datetime, ('1/1/' + convert ( varchar, (cast(left(right(XXXXXX+1000000,6),3) as varchar) + 1900))))),103)
如果您需要不同的公历格式,请将 103 值(位于末尾)替换为此处的适用值:https ://msdn.microsoft.com/en-us/library/ms187928.aspx