Link to home
Create AccountLog in
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.
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
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

Avatar of Kelly Martens
Kelly Martens


TimeSerial is throwing me off...
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

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