The normal method for converting dates to numbers is to pick a standard date (often called an Epoch Date) then count the number of days between the Epoch and the desired date.
Starting from the Epoch Date, you can add move forward the "number of days" to find thr original date.
The Examples below can be used to convert between popular Epoch based day-counters and the normal Gregorian Calendar.
Get Gregorian Date relating to an Epoch Day
Private Shared Function ModifiedJulianDay_To_Gregorian(ModifiedJulianDay) As Double) As Date
Return New Date(1858, 11, 17, 0, 0, 0, 0, 0).AddDays(ModifiedJulianDay)
End Function
Private Shared Function JulianDay_To_Gregorian(JulianDay As Double) As Date
' The JulianDay epoch date is in in a year over 4000 years BC.
' VB.Net functions do not expect dates from ancient pre-historic times
' The number 2400000.5, combined with a "modified" epoch in 1858 avoids various problems
Return New Date(1858, 11, 17, 0, 0, 0, 0, 0).AddDays(JulianDay - 2400000.5)
End Function
Private Shared Function UnixTimeStamp_To_Gregorian(utc As Double) as Date
' Although Unix Time Stamps count Seconds instead of Days, the principles are exaclty the same as day counts
' The Unix Time Stamp is NOT an Integer - Integer values will overflow after 19 Jan 2038
' Ensure your program logic always used Double or Long for all Unix Time Stamps
Return New Date(1970, 1, 1, 0, 0, 0, 0, 0).AddSeconds(UnixTimeStamp)
End Function
Private Shared Function ExcelDate_To_Gregorian(ExcelDate As Double) as Date
' Excel Time Stamp should not be used for dates prior to 1901
Return New Date(1899, 12, 30, 0, 0, 0, 0, 0).AddDays(ExcelDate)
End Function
Private Function Epoch1980_To_Gregorian(e1980 As double) As Date
Return New Date(1980, 1, 1, 0, 0, 0, 0, 0).AddDays(Epoch1980)
End Function
Private Function Epoch2000_To_Gregoian(e2000 As Double) As Date
Return New Date(2000, 1, 1, 0, 0, 0, 0, 0).AddDays(Epoch2000)
End Function
Get Epoch Day relating to Gregorian Date
Private Shared Function Gregorian_To_ModifiedJulianDay(dat_Greg As Date) As Double
Return DateDiff(DateInterval.Day, New Date(1858, 11, 17, 0, 0, 0, 0, 0), GregDate)
End Function
Private Shared Function Gregorian_To_JulianDay(dat_Greg As Date) As Double
' Calculate Julian Day via Modified Julian Day because dotNet compiler dislikes the year -4712 used in Julian Day epoch
Dim mjd As Double = DateDiff(DateInterval.Day, New Date(1858, 11, 17, 0, 0, 0, 0, 0), GregDate)
Return mjd + 2400000.5
End Function
Private Shared Function Gregorian_To_UnixTimeStamp(dat_Greg As Date) As Double
' The Unix Time Stamp is NOT an Integer - Integer values will overflow after 19 Jan 2028
' Ensure your program logic always used Double or Long for all Unix Time Stamps
' Unix Time Stamp counts Seconds since the Epoch, using similar logic to other Day Counting epochs.
Return DateDiff(DateInterval.Second, New Date(1970, 1, 1, 0, 0, 0, 0, 0), GregDate)
End Function
Private Shared Function Gregorian_To_ExcelDate(dat_Greg As Date) As Double
' This function is only reliable for Excel Dates after 1901
Return DateDiff(DateInterval.Day, New Date(1899, 12, 30, 0, 0, 0, 0, 0), GregDate)
End Function
Private Shared Function Gregorian_To_Epoch1980(dat_Greg As Date) As Double
Return New Date(1980, 1, 1, 0, 0, 0, 0, 0).AddDays(Epoch1980)
End Function
Private Shared Function Gregorian_To_Epoch12000(dat_Greg As Date) As Double
Return New Date(2000, 1, 1, 0, 0, 0, 0, 0).AddDays(Epoch1980)
End Function
DigitalDan.co.uk