March 20th, 2014, 05:49 PM
What is the equivalent of DateSerial (VB6) in C Sharp?
When I try to duplicate the results in C# that I get in VB6, it just does not come out the same (or even close).
Here is a snippet of VB6 code that I need to port over to C#.
The iYear = 1989, iMonth = 12, iDay = 18.
'Establish Julian date for this year and date
iYear = Year(dDate): iMonth = Month(dDate): iDay = Day(dDate)
fJulianDay0 = CDbl(DateSerial(iYear, 1&, 1&)) + 1.5
fJulianDate = CDbl(DateSerial(iYear, iMonth, iDay)) + 1.5
To solve for fJulianDay0 for the date of January 1, 1989, VB6 produces the result (32510.5).
To solve for fJulianDate for the date of December 18, 1989, VB6 produces the result of (32861.5).
I am unable to duplicate these results with C# and have been trying for weeks to solve this problem.
March 20th, 2014, 09:46 PM
Re: What is the equivalent of DateSerial (VB6) in C Sharp?
I don't know anything about Julian Dates, but from what a little research has turned up, it looks like your VB6 results produce invalid Julian Dates.
Doing a bing search, I found a post that suggest writing a DateTime extension method using the C# DateTime.ToOADate() method.
public static class DateTimeExtensions
public static double ToJulianDate(thisDateTime date)
return date.ToOADate() + 2415018.5;
I verified this by using the value returned by the Julian_Date wiki page.
||Value for 16:03, 20 March 2014 (UTC)
||12h Jan 1, 4713 BC
Using the March 20, 2014 value from above...
... it returns a value of 2456737.16875 which is close to the above value of 2456737.16902 with the difference being me not entering the exact seconds value.
var dt = new DateTime(2014, 3, 20, 16, 3,0);
var jd = dt.ToJulianDate();
It looks like the result returned by the VB6 code is different from the Julian date by 2415017.0 (based on both your examples when plugged into the ToJulianDate() extension).
Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.