javascript - ASP.NET MVC Client side time convert from model -


my cshtml page contains following code datetime, server time fetched sql.

<tbody>         @{             foreach (var item in model)              {                   <tr>                     <td>      @html.displayfor(modelitem => item.somedate)                                                               </td> 

the type of somedate property datetime. show time in respect client/browser time zone, preferably using javasscript. simples way of doing this? looked @ various similar answers none using html.displayfor , javascript. tried use code, doesn't work:

@(html.displayfor(modelitem => item.somedate)).tolocalestring(); 

or

var localdatetime = new date(item.somedate).tolocalestring(); @html.displayfor(localdatetime ) 

the above may require template not comfortable giving error.

i don't think can achieve without using javascript. here's try (using jquery convenience):

foreach (var item in model)  {      <tr>        <td data-iso="@item.somedate.tostring("o")"></td>    </tr> } 

afterwards, in document.ready parse date , set locale in td:

$("document").ready(function () {     $("td").each(function (index, elem) {         var date = new date($(elem).data("iso"));         $(elem).html(date.tostring());     }); }); 

this solution converts date using iso 8601 format, 1 of many formats supported javascrip date constructor.


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -