We help IT Professionals succeed at work.

.NET and TimeSerial from vb6

150 Views
Last Modified: 2018-11-12
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

Chief Technology Ninja
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
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...
CERTIFIED EXPERT
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.
CERTIFIED EXPERT
Most Valuable Expert 2015
Distinguished Expert 2018

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

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions