DateSerial (vb6) equivalent to type double

webbiz

Member
Joined
Oct 15, 2011
Messages
21
Programming Experience
10+
I'm converting some old code I have in VB6 into C#. I'm new to C# so I'm having trouble finding via search an answer to this question.

Here is the VB6 code snippet:

Dim fJulianDay0 as Double
Dim fJulianDate as Double
Dim iYear as Integer
Dim iMonth as Integer
Dim iDay as Integer

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


This is my attempt to convert to C#:

double fJulianDay0, fJulianDate;
int iYear, iMonth, iDay;

iYear = DateTime.Parse(dDate).Year;
iMonth = DateTime.Parse(dDate).Month;
iDay = DateTime.Parse(dDate).Day;


Here is where I am stuck:

Changing...

fJulianDay0 = CDbl(DateSerial(iYear, 1&, 1&)) + 1.5
fJulianDate = CDbl(DateSerial(iYear, iMonth, iDay)) + 1.5

...to C#.

I'm thinking...

fJulianDay0 = (double)(DateTime(iYear, 1, 1)) + 1.5);

...but this gives me an error.

Help please. :ambivalence:

TIA
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,136
Location
Sydney, Australia
Programming Experience
10+
Rather than just saying "here's my code" and expecting us to work out what it does, it's a good idea to explain exactly what it is that you're trying to achieve. There's every chance that a C# developer has no experience with VB6 so why would they know what DateSerial does? I've been coding in VB.NET and C# for 9 years but I've never used VB6 so even I don't know what it does. If you explain what it is that you're trying to achieve then we can try to provide the best solution to achieve that in C# and it really doesn't matter what the VB6 code looks like.

Having now looked up DateSerial, I see that it simply creates a Date from a year, month and day. The .NET DateTime structure has a constructor that does exactly that, so that's what you should use.

You're not going to be able to cast a DateTime as a double in C# though. The VB CDbl operator doesn't just cast, but does some massaging if required to convert a value of one type to another. C# considers casting and converting to be strictly different operations. What exactly does the value returned by CDbl when passed a Date in VB represent and how exactly does it relate to the conventional date value?
 

webbiz

Member
Joined
Oct 15, 2011
Messages
21
Programming Experience
10+
Hello.

I'm sorry about the way my question is worded. Frankly, while I understand much of VB6, I don't necessarily understand what some parts of code are trying to accomplish and am simply converting code over from useful routines in VB6 that is in my old progs.

I suspect that the code is used to create a JULIAN DATE of type double from integers Year, Month and Day and then add 1.5 to it. Why add 1.5? I dunno.

I believe CDbl converts the date 'string' ?? to double value.

How about this approach?

dDate is a string, btw.



Found this snippet after much searching:

public static long ToJulian(DateTime dateTime)
{
int day = dateTime.Day;
int month = dateTime.Month;
int year = dateTime.Year;
if (month < 3)
{
month = month + 12;
year = year - 1;
}
return day + (153 * month - 457) / 5 + 365 * year + (year / 4) - (year / 100) + (year / 400) + 1721119;
}



double fJulianDay0, fJulianDate;

//convert string date to datetime

DateTime date = Convert.ToDateTime(dDate);

//Establish Julian date for this year and date

DateTime dBegYear = new DateTime(date.Year, 1, 1);

fJulianDay0 = ToJulian(dBegYear) + 1.5;
fJulianDate = ToJulian(date) + 1.5;

Was my approach valid? I know that function ToJulian returns a long. Would adding 1.5 to it make it a double and thus valid to assign to a double variable?

(PS: I am still just getting to learn the debugging tools available. Not yet sure how to just test a snippet of code so just looking to see if my LOGIC way of thinking is okay.)


Thanks.
 
Last edited:

webbiz

Member
Joined
Oct 15, 2011
Messages
21
Programming Experience
10+
I know this is an old thread, but I still haven't solved this problem and left it alone for awhile. Now I'm back to it.

iYear is an integer holding the year. In this example, the iYear = 1989.

This next line returns a double value that represents the date "1989", "1", "1". January 1, 1989.

C#:
fJulianDay0 = CDbl(DateSerial(iYear, 1&, 1&)) + 1.5
The double result for fJulianDay0 is 32510.5

I'm trying to figure out how to do the same with C#. A search says DateTime has a DateSerial method, but I have yet to figure out how to use it. It does not show up in my intellisense.

The next line is:

C#:
fJulianDate = CDbl(DateSerial(iYear, iMonth, iDay)) + 1.5
Where iYear is 1989, iMonth = 12 and iDay = 18.

fJulianDate equals 32861.5

I've tried so many things and still can't get C# to return the same values (32510.5 and 32861.5) as the VB code does with DateSerial.

Anyone point me in the right direction please?

Thanks.
 
Top Bottom