CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    May 2009
    Posts
    34

    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#.

    Code:
    '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
    The iYear = 1989, iMonth = 12, iDay = 18.

    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.

    Any suggestions?

    TIA

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    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.
    http://stackoverflow.com/questions/5...-tooadate-safe

    Code:
    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.
    Name Epoch Calculation Value for 16:03, 20 March 2014 (UTC) Notes
    Julian Date 12h Jan 1, 4713 BC 2456737.16902

    Using the March 20, 2014 value from above...
    Code:
    var dt = new DateTime(2014, 3, 20, 16, 3,0);
    var jd = dt.ToJulianDate();
    
    ... 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.

    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).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured