Load DateTime in c # in dd / mm / yyyy format

1

Good day! I'm doing a program of loading and showing movies, The movie class has an attribute of type DateTime release date. My problem is that, when I upload the release date in dd / mm / yyyy format, I do not take it. I have to load yyyy / mm / dd and also show it to me that way. My question is if there is any way not only to show me the date in the dd / mm / yyyy format but also to allow me to upload it in that format! Thank you very much!

Movie Class:

public class Peliculas
{

    // Propiedades
    public int Id { get; private set; }
    public String Nombre { get; set; }
    public DateTime FechaSalida { get; set; }
    public String Pais { get; set; }
    public static int globalPeliculaId;

    // Metodos


    // Constructores
    public Peliculas()
    {
        this.Nombre = "";
        this.FechaSalida = DateTime.Now;
        this.Pais = "";
        this.Id = Interlocked.Increment(ref globalPeliculaId);
    }

    public Peliculas(String nombre, int dia, int mes, int anio, String pais)
    {
        this.Nombre = nombre;
        this.FechaSalida = new DateTime(dia, mes, anio); //Validar en Controlador que ingresen aaaa, mm y dd y no otro formato. 
        this.Pais = pais;
    }
}

Controller films creation function:

// GET: Peliculas/Create
public ActionResult Create()
{
    return View();
}

// POST: Peliculas/Create
[HttpPost]
public ActionResult Create(Peliculas pelicula)
{
    try
    {
        RepoPeliculas.listado.Add(pelicula);

        return RedirectToAction("Listado");
    }
    catch
    {
        return View();
    }
}

Create View:

//porcion de codigo de la fecha)

<div class="form-group">
    @Html.LabelFor(model => model.FechaSalida, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.FechaSalida, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.FechaSalida, "", new { @class = "text-danger" })
    </div>
</div>
    
asked by Maria Laura 28.11.2017 в 13:49
source

4 answers

1

Good day, you can do it in the razor code that you use easily using format string

<div class="form-group">
    @Html.LabelFor(model => model.FechaSalida, htmlAttributes: new { @class = "control-label col-md-2", [email protected]("{0:MM-dd-yyy}",  model.FechaSalida) })
    <div class="col-md-10">
        @Html.EditorFor(model => model.FechaSalida, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.FechaSalida, "", new { @class = "text-danger" })
    </div>
</div>

Try it and comment

    
answered by 29.11.2017 в 14:02
0

Well I want to take this question to show features of ASP.Net MVC that sometimes we do not know what they are but they help us a lot. MariaLaura wants all the dates to be shown in the form dd / MM / yyyy, and the answer from Ingwvanegas is not bad, but suppose we have many views which show dates, this means having to enter each view and modify it to achieve what we want. But ASP has a way of doing this for all views. In the Views / Shared directory we created a folder named DisplayTemplates and created the Partial DateTime.cshtml view with the following content:

Views/Shared/DateTime.cshtml

@model DateTime
@Model.ToString("dd MMMM, yyyy")

In this way we have defined a template for our DisplayFor helper so that if we call the following:

    <td>
        @Html.DisplayFor(modelItem => item.Fecha)
    </td>

would come out: 04 December, 2017

Just as we define a template for DisplayFor, we can make one for EditorFor. This time we created the EditorTemplates directory in Views / Shared / with a Partial view with the same name DateTime and inside:

@inherits System.Web.Mvc.WebViewPage<DateTime?>
@if (Model.HasValue)
{
@Html.TextBox("", String.Format("{0:dd/MM/yyyy}", Model.Value), new { @class = "form-control datepicker", @type = "datetime " })
}
else
{
@Html.TextBox("", String.Format("{0:dd/MM/yyyy}", DateTime.Now), new { @class = "form-control datepicker", @type = "datetime" })
}

and in the model we should put this:

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime Fecha { get; set; }

by creating these templates we will see the date and insert the date in the format we want anywhere in our app. I hope it helps you

    
answered by 04.12.2017 в 22:17
-1

If you are only going to show the date in a textbox you only have to concatenate your variables. variables of type string txt_fec.text = day + "/" + month + "/" + year

variables of type int txt_fec.text = convert.ToString (day) + "/" + convert.ToString (month) + "/" + convert.ToString (year)

    
answered by 30.11.2017 в 16:28
-2

You can use the ParseExact function of the DateTime class to parse your date in the format you want and to read it in the same format you can pass it as a parameter to the ToString function, the same format and the Date Format Provider, in our Invariant case so that you omit the default one.

var dateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;
var date = DateTime.ParseExact("07/08/2014", "dd/MM/yyyy", dateTimeFormat);
Console.WriteLine(date.ToString("dd/MM/yyyy", dateTimeFormat));

//CultureInfo está en System.Globalization
    
answered by 28.11.2017 в 14:07