user1935160 user1935160 - 1 year ago 140
C# Question

Why does DateTime.Now.TimeOfDay.ToString("HH:mm:ss.ffffff") throw FormatException?

I'm having a similar problem with FormatException being thrown. My code is simply:

void Orders_OnSubmit()
DateTime CurrentTime = DateTime.Now;
rtbAdd( "Submitted on " + CurrentTime.Date.ToString("MM/dd/yyyy") + " at " + CurrentTime.TimeOfDay.ToString("HH:mm:ss.ffffff") );

void rtbAdd(String S)
DefaultDelegate del = delegate()
rtb.AppendText(S + "\n");

What's wrong here? Is this a threading issue?

Answer Source

There's no need to explicitly access the Date and TimeOfDay properties of the DateTime instance. You can simplify your code like so:

rtbAdd(String.Format("Submitted on {0:MM/dd/yyyy} at {0:HH:mm:ss.ffffff}", DateTime.Now));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download