Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

formatting DateTime error "No overload for method 'ToString' takes 1 arguments"

In MVC Razor view, I am trying to format a DateTime field to display time only. Using below code I am getting error "No overload for method 'ToString' takes 1 arguments"

<td>@(Html.DisplayFor(m=>row.LastUpdatedDate).ToString("HH:mm:ss"))</td>

Any help please what is causing this error and how to fix it ?

Thanks for your help.

like image 891
Toubi Avatar asked Jan 31 '26 00:01

Toubi


2 Answers

Try use a System.ComponentModel.DataAnnotations.DisplayFormat attribute on the property in the model.

...
[DisplayFormat(DataFormatString = "{0:HH:mm:ss}")]
public DateTime LastUpdatedDate{get; set;}
...
like image 106
PSL Avatar answered Feb 02 '26 12:02

PSL


DisplayExtensions.DisplayFor returns MvcHtmlString which does not have ToString with one argument thus causing error you see.

You may not even need DisplayFor if you need just to show the date time value:

<td>@row.LastUpdatedDate.ToString("HH:mm:ss")</td>
like image 43
Alexei Levenkov Avatar answered Feb 02 '26 12:02

Alexei Levenkov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!