Unsolicited Truncation of Time

Asked

Viewed 53 times

0

I have a model designed to record a time used in a task:

public TarefaHoraPendente()
    {
        HorasAcumuladas = new TimeSpan(0, 0, 0);
        Pausada = false;
    }

    public int ID { get; set; }

    [Required(ErrorMessage = "Este campo é obrigatório")]
    [StringLength(10, ErrorMessage = "Qtde. Máx.: 10 caracteres")]
    public string Identificador { get; set; }        

    [Required, DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Data { get; set; }

    [Required, DataType(DataType.Time)]
    public DateTime HoraInicial { get; set; }

    [DataType(DataType.Time)]
    public DateTime HoraReinicio { get; set; }

    public TimeSpan HorasAcumuladas { get; set; }

    public bool Pausada { get; set; }        
}

When I create a new entity of this model some values are already filled by the Controller:

public ActionResult Create()
    {
        DateTime hoje = new ObtendoDataeHoraLocal();
        TarefaHoraPendente tarefaHora = new TarefaHoraPendente();
        tarefaHora.Data = hoje;
        tarefaHora.HoraInicial = hoje;
        tarefaHora.HoraReinicio = hoje;            

        return View(tarefaHora);
    }

So everyone has the same information, for example: {07/04/2017 10:20:43} with Date, time, minute and SECONDS.

Goes to View :

@model Models.Auxiliares.TarefaHoraPendente

@{
    ViewBag.Title = "Projeto";
}

<h2>Iniciar marcação de horas</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        @Html.HiddenFor(model => model.Data)

        <dl class="dl-horizontal">
            <dt>
                @Html.DisplayNameFor(model => model.Data)
            </dt>

            <dd>
                @Model.Data.ToString("dd/MM/yyyy")
            </dd>
        </dl>                

        <div class="form-group">
            @Html.LabelFor(model => model.Identificador, "Identif.(Máx.10 caract.)", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Identificador, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Identificador, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HoraInicial, "Hora inicial", htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HoraInicial, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HoraInicial, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Iniciar contagem" class="btn btn-default" />
            </div>
        </div>
    </div>
}

And in View, the Start Time Field no longer shows the seconds and I don’t see anything that indicates this truncation.

Can anyone tell me what I’m doing wrong.

  • I believe this is the line below: @Model.Data.Tostring("dd/MM/yyyy")

  • I was talking about the format of the Initial Time field. This code is from the Date field. Presented in standard format. It’s not about the problem I presented.

1 answer

1


In his View you’re passing a DateTime complete and the EditorFor() is with the guy’s field Time. It’s the same thing as you doing it:

<input type="time" value="04/07/2017 01:59:00" />

This will not work at all as it is passing a full date to a field that accepts only "time".

To solve this, you can use the DisplayFormat, like you used on the property Data of your Model. However, put the format as Time, in this way:

[DisplayFormat(DataFormatString = "{0:HH:mm:ss}", ApplyFormatInEditMode = true)]
[Required, DataType(DataType.Time)]
public DateTime HoraInicial { get; set; }

So you are specifying that you only want date time.

Another point is that the type input time shows/edits only the time and minutes by default. To display seconds, add the attribute step, as the example below:

<p>Sem step</p>
<input type="time" value="01:59:32" />

<p>Com step</p>
<input type="time" value="01:59:32" step="1" />

See the functional example in the Dotnetfiddle.

  • Here displayed seconds in both forms.

  • 2

    @diegofm One can be edited, the other cannot

  • 1

    Thanks, that’s right. Just put the Format. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.