Format date in Viewbag

Asked

Viewed 557 times

1

I have a Viewbag returning two dates in a Dropdown, but they are returning with the same format that were saved in the database( MM/dd/yyy HH:mm:ss). I need them to return in the pattern(dd/MM/yyyy).

My Viewbag is like this:

 ViewBag.Ferias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao != null)
               .Select(x => x.DtInicioPeriodo + " à " + x.DtFimPeriodo);

And in my view, I call her that way:

@Html.DropDownList("Ferias", new SelectList(ViewBag.Ferias, "Ferias"))

The result is this:

Jun 22 2010 12:00AM to Feb 19 2014 12:00AM

2 answers

1

If the data is nullable and some argument can have a null value:

Would look like this:

ViewBag.Ferias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao != null)
           .Select(x => 
            x.DtInicioPeriodo.HasValue ? x.Value.DtInicioPeriodo.ToString("dd/MM/yyyy") : "" 
             + " à " + 
            x.DtFimPeriodo.HasValue ? x.Value.DtFimPeriodo.ToString("dd/MM/yyyy") : "");
  • I have tried, but I get the following error: "No Overload for method 'Tostring' takes 1 Arguments"

  • Try calling a .ToList(), see if this works: ViewBag.Ferias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao != null).ToList().Select(x => x.DtInicioPeriodo.ToString("dd/MM/yyyy") + " à " + x.DtFimPeriodo.ToString("dd/MM/yyyy"));

  • I get the same mistake with . Tolist()

  • Um, the guy DtInicioPeriodo is DateTime or string?

  • the two fields are of type Datetime

  • Apparently the guy isn’t DateTime, maybe DateTime? (nullable)?. Try now, as I changed the answer.

  • I get this error now: does not contain a Definition for value and no Extension method value Accepting a first argument of type could be found(are you Missing using Directive or an Assembly Reference)

  • Data is Datetime? (nullable) yes

  • I changed the answer again, see if that’s it.

  • I continue with the same error: does not contain a Definition for value and no Extension method value Accepting a first argument of type could be found(are you Missing using Directive or an Assembly Reference)

Show 5 more comments

0


I was able to separate the two attributes, and concatenate them into the view. Stayed like this:

Controller:

ViewBag.Ferias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao == null)
            .Select(x => x.DtInicioPeriodo).ToList();
            ViewBag.FimFerias = funcionarioFeriasRepository.Lista.Where(r => r.CdMatricula == matricula && r.SqContrato == contrato && r.DtInicioConcessao == null)
            .Select(x => x.DtFimPeriodo).ToList();

View:

<select>
                                @foreach (var date in ViewBag.Ferias)
                                {
                                    foreach (var fim in ViewBag.FimFerias) { 
                                    <option>
                                        @Convert.ToDateTime(date).ToShortDateString() à @Convert.ToDateTime(fim).ToShortDateString()
                                    </option>
                                    }
                                }

If anyone has a better alternative, please post here.

Browser other questions tagged

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