vb6 DateSerial to C# Equivalent

webbiz

Member
Joined
Oct 15, 2011
Messages
21
Programming Experience
10+
I'm converting old VB6 code to C# which I'm a newbie.

lTempDays = Int(DateSerial(Year(startdate) + increment, Month(startdate), Day(startdate)))


In C# variables we have:

long lTempDays;
(increment passed as long)
(startdate passed as string)

DateTime date = Convert.ToDateTime(startdate);

I know I can get date.Year, date.Month and date.Day to pass to a DateSerial type method.

But what is the DateSerial equivalent to vb6 DateSerial to do the above and assign to lTempDays?

Thanks.
 
You shouldn't even need to ask that question. You're using the DateTime type so you should have already read the MSDN documentation for that type. If you had then you'd have seen that there is a constructor that takes year, month and day values, just as the DateSerial function. The MSDN documentation should always be your first port of call and, given that it's a couple of clicks away via the Help menu on VS, there's reason or excuse for not using it. If you are unfamiliar with a type or member then read the documentation for that type or member. The information you wanted in this case was at your fingertips and that will be the case on many other occasions too.
 
Did I mention that I'm a "newbie"?

I've read a lot on C# and watched a ton of YouTube videos. But you made an assumption that I knew I could do what you say.

For one, I had no idea that I should look deeper into DateTime type. I'm from the VB6 world. We did not dig into class types.

I did a search on the web for DateSerial and C# and got nothing useful. You see, I did not connect DateTime to DateSerial.

Anyway, thank you for your reply.

:)
 
Confused

Okay, I'm really confused.

I went to Help and clicked on MSDN forums.

Then after clicking all over the place trying to locate the info, I noted a search box and typed in DateTime (which I did not associate with DateSerial in the beginning).

Now I have a whole list of methods that I had already seen before with Intellisense, but NONE of them says anything about SERIAL.

Reading all the descriptions, I notice that ToBinary "serializes" to a 64-bit binary value. But is this what the DateSerial in VB6 does?

Anyway, I suppose I'm going to have to create a test VB6 app and see if I get apples to apples trying out these methods.

My original question was to find out if anyone knew which method was a direct substitute for DateSerial. Getting the answer does not appear to be as easy as suggested, but I'll keep at it.

Thanks.
 
I've searched and spent a great amount of time trying to answer this question for myself.

My VB6 text code looks like this:

C#:
Private Sub Command1_Click()
Dim sDate As Date
Dim lDate As Long




    sDate = Text1.Text
    
    lDate = DateSerial(Year(sDate), Month(sDate), Day(sDate))
    
    Text1.Text = lDate




End Sub

Then trying to fine a method in C# that would return the same result as the VB6 example, I tried several methods, including Ticks as was suggested to get the DateTime to the Long Variable. But this did not give me the same long value as the VB6 code did.

C#:
       private void button1_Click(object sender, EventArgs e)
        {
            DateTime Date;
            long lDate;


            Date = Convert.ToDateTime(textBox1.Text);


            DateTime t = new DateTime(Date.Year, Date.Month, Date.Day);


            lDate = t.Ticks;


            textBox1.Text = Convert.ToString(lDate);


        }

Now I tried to find the "easy to find" answer to this problem as suggested, but seriously, I don't think my question was that bad to begin with.

Just so you know, I never ask a question until I've tried really hard to figure it out for myself.

Clearly I'm missing something. But now I get the impression that beginners should not ask beginning questions here.
 
DateSerial Function (Visual Basic) said:
Returns a Date value representing a specified year, month, and day, with the time information set to midnight (00:00:00).
DateTime.Date Property (System)
Property Value said:
A new object with the same date as this instance, and the time value set to 12:00:00 midnight (00:00:00).
This appear to be the same result you're seeking, although you don't even need this if the Date value was initially created from just a y/m/d value.

As for converting the Date value to a String value, look to the various ToString methods DateTime structure has to offer.
 
Hello John.

The VB6 code returns 41618 (into a Long variable when passed the date "12/10/2013") whereas I cannot seem to get this same value using DateTime.Date.

I tried ...



lDate = t.Date;
lDate = Convert.ToInt64(t.Date);

Can't seem to get any of this to work.

I'm downloading a VB6 to C# converter to see if it may provide me some clues.

Thanks.
 
I don't know if there is a C# equivalent to VB6 DateSerial(), but when I used a VB6 to C# converter app, it provided the solution as follows:

using Microsoft.VisualBasic;
using VB6 = Microsoft.VisualBasic.Compatibility.VB6.Support;

(be sure to add the references)

C#:
       private void button1_Click(object sender, EventArgs e)
        {
            


            System.DateTime sDate = DateTime.Parse("12/10/2013");


            int lDate = Convert.ToInt32(DateAndTime.DateSerial(sDate.Year, sDate.Month, DateAndTime.Day(sDate)).ToOADate());


            textBox1.Text = lDate.ToString();






        }

This provided the expected output of 41618.

:)
 
Did I mention that I'm a "newbie"?

I've read a lot on C# and watched a ton of YouTube videos. But you made an assumption that I knew I could do what you say.

Yes, I assumed that you were able to click the Help menu on your IDE. VB6 already has a bad name so blaming the fact that you are "from the VB6 world" as a reason for not being able to use Help is not doing it any favours. You don't need any programming experience to search and read documentation.
 
Perhaps not. But it has been resolved that answering my question was not as easy as you implied by just clicking HELP.

Apparently DateTime did not provide me with the solution, as you can see from my last reply that shows it was a bit more complicated than that.

So to be fair, your initial response made an assumption that was not applicable and could have been more helpful rather than insinuating I was being lazy.

Besides, you could say to everyone who asks a question that they can "search and read documentation." But at some point you just have to stop and ask, don't you think?

Thanks anyway.
 
Last edited:
System.DateTime sDate = DateTime.Parse("12/10/2013")
is the same value as
DateAndTime.DateSerial(sDate.Year, sDate.Month, DateAndTime.Day(sDate))
and latter is thus unnecessary.

I failed to see first that you wanted to convert the date to integer (then to display it as string), but you should have seen that DateSerial does nothing but create a date value with time set to 0 and has no use for you now.
 
I only displayed it as a string to see the results.

The whole thing is to convert my old VB6 code to C#, since I'm trying to learn C# and want no reason to keep having to go back to VB6 just because I'm comfortable with it in a pinch.

With the original VB6 code, the result of DateSerial is placed into a LONG variable.

The value returned for "12/10/2013" is suppose to be 41618, as it is passed to another procedure as an int.

I have been unable to convert the return value of DateTime to a Long or Int type that would provide me with the exact same result (41618) as the VB6 DateSerial line.

Thoughts?

Thanks.
 
I have been unable to convert the return value of DateTime to a Long or Int type that would provide me with the exact same result (41618) as the VB6 DateSerial line.
You have already done that, you're just to realize it.
var date = DateTime.Parse("12/10/2013"); // culture dependent string format, for instance US
var oleDate = date.ToOADate(); // value 41618.5
var intDate = Convert.ToInt32(oleDate); // value 41618
 
Well that was what I was asking for, to find out HOW to do this in my very first post question but got admonished to read MSDN through Help (which was not help to me at all).

This is the first mention of using .ToOADate, or to use type "var". I was getting errors just trying to convert.

As a newbie trying to learn how to solve this problem, that's what I was hoping to find out because this solution was nowhere to be found in my online searches.

Would you say this is simply a failure to communicate? :)

Thank you very much!
 
This is the first mention of using .ToOADate,
It was you who first mentioned ToOADate method in post 8.
or to use type "var".
var is not a type, it's a type inferring variable declaration, meaning variable gets the type of the assigned expression. var (C# Reference) I'm used to type inference from VB.Net and continue to use that in C#, also because I already have a good understanding of data types. Actually I was disappointed with C# IDE at first, because I couldn't see the inferred type by hovering the variable name like in VB.Net, until I found that in C# you have to hover 'var' to see it.
 
Back
Top Bottom