Avatar of Kelly Martens
Kelly Martens
 asked on

.NET and TimeSerial from vb6

These two lines of vb6 code I am trying to translate to C#:

mskFromDate.Text = Format(Date - TimeSerial((iNumDaysBefore * 24), 0, 0), "Short Date")
    mskToDate.Text = Format(Date + TimeSerial((iNumDaysAfter * 24), 0, 0), "Short Date")

I have this so far. I know it is not right.

var dt = string.Empty;
                dt = Convert.ToString(DateTime.Now.ToOADate() - (NumDaysBefore * 24));
                this.dtFromDate.Text = Convert.ToDateTime(dt).ToShortDateString();

Anybody have thoughts? I can't believe in vb6 they let folks subtract dates and numbers.
Visual Basic.NET.NET ProgrammingC#Visual Basic Classic

Avatar of undefined
Last Comment
Gustav Brock

8/22/2022 - Mon
Chinmay Patel

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Gustav Brock

That VBA can be reduced to:

// Days to add/deduct.
int numDaysBefore = 2;

mskFromDate.Text = DateTime.Today.AddDays(-numDaysBefore).ToShortDateString();
mskToDate.Text = DateTime.Today.AddDays(numDaysBefore).ToShortDateString();

Open in new window

Kelly Martens

TimeSerial is throwing me off...
Gustav Brock

Not sure what you mean - but the original code could have been reduced as well:

mskFromDate.Text = Format(DateAdd("d", -iNumDaysBefore, Date), "Short Date")
mskToDate.Text = Format(DateAdd("d", iNumDaysAfter, Date), "Short Date")

Open in new window

All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Kelly Martens

Thank you all.
Gustav Brock

You are missing something. That "solution" will add NumDaysBefore * 24 days, not NumDaysBefore days.