-1

I want to change the content of a model item in the HTML page. I am saving datetime, in database, in the next format: yyyyMMddHHmmss

and i want to display, in the HTML page, the next format: dd-MM-yyyy HH-mm-ss

How can i do that?

<tr>
  <td class="labels">
    <label>Data/Hora Token Telemóvel:</label>
  </td>
  <td>
    @Html.TextBox("txtBoxNome", @Html.DisplayFor(modelItem => item.cdts_token_phone), new { @readonly = "readonly", @disabled = "disabled"})
  </td>
</tr>
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
  • Format the date in your controller before sending it to the view. You can see [in another question](http://stackoverflow.com/questions/3025361/c-sharp-datetime-to-yyyymmddhhmmss-format) how to format the date. – Drew Kennedy Jan 19 '16 at 17:31
  • @DrewKennedy thanks for the response. i know how the format is done, what i dont know is how do i send to the view. i am filling the textbox with the data coming from the database – akunamatata Jan 19 '16 at 17:34
  • What DB management system are you using? Why don't you use a proper type like DATETIME or DATETIME2? – Alexei - check Codidact Jan 19 '16 at 17:36
  • Do you have a view model for the data being sent to the view? – Drew Kennedy Jan 19 '16 at 17:37
  • and at the html page i have a button that do a process and refreshes the page. and doing that, it will automatically fill the textbox with data from the database. so, i want that each time the html is renderized, it uses always the date formatted as i described: dd-MM-yyyy HH-mm-ss – akunamatata Jan 19 '16 at 17:38
  • @Alexei i have to use the date format yyyyMMddHHmmss to store in the db – akunamatata Jan 19 '16 at 17:39
  • @DrewKennedy i have a view where i show a list with the fields of a table from the db. and associated to that view i have a controller – akunamatata Jan 19 '16 at 17:44

2 Answers2

1

If I understood correctly, the problem is converting the string from your database into a more proper string in the view (via the viewmodel):

You can create a property in your model that parses your format using TryParse or TryParseExact (great suggestion from here).

[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy HH-mm-ss}"] 
public DateTime MyDateTime
{
   get
   {
       // should be defined as a constant elsewhere
       string pattern = "yyyyMMddHHmmss";
       DateTime dt;
       if (DateTime.TryParseExact(text, pattern, CultureInfo.InvariantCulture, 
                           DateTimeStyles.None, out dt))
          return dt;

       // return a value when format is invalid
   }
}

DisplayFormat attribute should help you directly displaying the data in your desired format.

Community
  • 1
  • 1
Alexei - check Codidact
  • 22,016
  • 16
  • 145
  • 164
  • thanks. you show me that way ahah i went to the model and added a private function to convert to the format i want – akunamatata Jan 20 '16 at 10:08
0

ok i have done it with help of Alexei. here is my solution:

    [EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=true)]
    [DataMemberAttribute()]
    public global::System.String cdts_token_phone
    {
        get
        {
            return cdtsToDT(_cdts_token_phone);
        }
        set
        {
            Oncdts_token_phoneChanging(value);
            ReportPropertyChanging("cdts_token_phone");
            _cdts_token_phone = StructuralObject.SetValidValue(value, true, "cdts_token_phone");
            ReportPropertyChanged("cdts_token_phone");
            Oncdts_token_phoneChanged();
        }
    }

private string cdtsToDT(string cdtsUT)
    {
        if(string.IsNullOrEmpty(cdtsUT))
            return string.Empty;

        DateTime _newDT = new DateTime(int.Parse(cdtsUT.Substring(0, 4)), int.Parse(cdtsUT.Substring(4, 2)),
                int.Parse(cdtsUT.Substring(6, 2)), int.Parse(cdtsUT.Substring(8, 2)),
                int.Parse(cdtsUT.Substring(10, 2)), int.Parse(cdtsUT.Substring(12, 2)));
        string cdts = _newDT.ToString("dd-MM-yyyy HH:mm:ss");
        return cdts;
    }