.NET and TimeSerial from vb6

Kelly Martens
Kelly Martens used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Chief Technology Ninja
Distinguished Expert 2018
Commented:
Hi Kelly,

I am not exactly following the VB6 code, but can you please give this one a try
this.dtFromDate.Text  = DateTime.Now.AddDays(NumDaysBefore * 24 *-1)).ToShortDateString();

Open in new window


Regards,
Chinmay.
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
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

Author

Commented:
TimeSerial is throwing me off...
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
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

Author

Commented:
Thank you all.
Most Valuable Expert 2015
Distinguished Expert 2018

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial