0

I have datetime data type in my asp.net core mvc application it displays mm/dd/yyyy --:-- -- doest take input from datepicker . Readonly mode even displays this mm/dd/yyyy --:-- -- instead datetime passing to the model.what is solution there way to remove this data?

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
  • 2
    Post your code - both HTML/Javascript and server-side. What date picker did you use? DateTime has no format, it's a binary value. Formats apply only when converting to text or parsing text. Even if you use a Javascript datepicker that returns dates as strings, ASP.NET Core will parse strings in the ISO8601 format. – Panagiotis Kanavos Dec 27 '19 at 14:47
  • @PanagiotisKanavos I would guess that they are using ` – Heretic Monkey Dec 27 '19 at 20:57

3 Answers3

1

The default value format of datepicker is yyyy-mm-dd , if you set the displayFormat of DateTime type property , you should set the type="text" to the input , or you maybe get the error like The specified value "02/12/1995" does not conform to the required format, "yyyy-MM-dd". Here is a example:

Model:

 public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public int Age { get; set; }
    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime Birth { get; set; } 
}

View:

<div class="row">
<div class="col-md-4">
    <form asp-action="Edit">
        <div asp-validation-summary="ModelOnly" class="text-danger"></div>
        <input type="hidden" asp-for="Id" />
        <div class="form-group">
            <label asp-for="Name" class="control-label"></label>
            <input asp-for="Name" class="form-control" />
            <span asp-validation-for="Name" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Email" class="control-label"></label>
            <input asp-for="Email" class="form-control" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Age" class="control-label"></label>
            <input asp-for="Age" class="form-control" />
            <span asp-validation-for="Age" class="text-danger"></span>
        </div>
        <div class="form-group">
            <label asp-for="Birth" class="control-label"></label>
            <input type="text" asp-for="Birth" class="form-control" id="datepicker" />
            <span asp-validation-for="Birth" class="text-danger"></span>
        </div>

        <div class="form-group">
            <input type="submit" value="Save" class="btn btn-primary" />
        </div>
    </form>
</div>
</div>

@section Scripts {
@{await Html.RenderPartialAsync("_ValidationScriptsPartial");}

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/datepicker/0.6.5/datepicker.js">
</script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#datepicker").datepicker({
            format: 'dd/mm/yyyy'
        })
    });
</script>   
}

Result:

enter image description here

Xueli Chen
  • 11,987
  • 3
  • 25
  • 36
0

As my understanding from your question, you want to input date using datepicker and display the only date mm/dd/yyyy part instead of mm/dd/yyyy --:-- --.

You can achieve this by using data annotation with your model.

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

And in your razorview, you can Html helper to input and show date.

@Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })

And for readonly show

@Html.DisplayFor(modelItem => item.Date)

Please check out this question for more understanding.

habib
  • 2,366
  • 5
  • 25
  • 41
-1

As Kanavos commented above, formats only apply when converting to text or parsing text.

If you wish for individual pieces of your DateTime object to be shown and others to be hidden, you can do something like:

 var newFormat = dateTimeObj.ToString("<desired format>")

and replacing the "< desired format >" with one listed in the MS docs here: https://learn.microsoft.com/en-us/dotnet/api/system.datetime.tostring?view=netframework-4.8