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

前端 未结 2 1364
萌比男神i
萌比男神i 2021-01-07 23:00

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

void Orders_OnSubmit()
{
   DateTime CurrentTime = DateTime.Now;
   rtbAd         


        
相关标签:
2条回答
  • 2021-01-07 23:27

    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));
    
    0 讨论(0)
  • 2021-01-07 23:34

    TimeOfDay is of type TimeSpan and it has different formatting options than DateTime. You also need to escape the colon (:)

     currentTime.TimeOfDay.ToString("hh\\:mm\\:ss\\.ffffff") 
    

    Your sample tried to use the "HH" format which is defined for DateTime, but not for TimeSpan.

    0 讨论(0)
提交回复
热议问题