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:
Back
Top Bottom