asp.net mvc 4 - input datetime set string.format to model item -


i've input of type datetime

   <input type="datetime" id="data_end_@id" value="@string.format("dd/mm/yyyy",item.data_end_prezzatura.tostring())" /> 

i need set model item value in datetime format dd/mm//yyyy

what right sintax?

thank you!

in end solution

@{                 string value_d_s = "";                 datetime? dateornull = item.data_end_prezzatura;                 if (dateornull != null)                 {                     datetime date_d_s = dateornull.value;                     value_d_s = date_d_s.tostring("dd/mm/yyyy");                 }               }              <input type="datetime" id="data_end_@id" value="@value_d_s" />      

use overload of datetime.tostring() method format date.

<input type="datetime" id="data_end_@id" value="@item.data_end_prezzatura.tostring("dd/mm/yyyy")" /> 

if item.data_end_prezzatura string instead of datetime, need use datetime.tryparse():

@{     datetime test;     datetime.tryparse(item.data_end_prezzatura, out test); } <input type="datetime" id="data_end_@id" value="@test.tostring("dd/mm/yyyy")" /> 

note need handle case when tryparse fails.


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -