0

I'm working with asp.net core in visual studio 2017

I will need to use C#, MVC, or SQL to solve this

I'm creating a table with a foreach statement (below)

        <table>
           <tbody>
                @foreach (var item in Model)
                {
                <tr>

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

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

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

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

                </tr>
                }
            </tbody>
        </table>

and I'm trying to modify this model data to display only the Date.

        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }

How would I do this so that all of my model items are effected?

Here are my controller actions

 public IActionResult Create()
        {
            return View();
        }

and

        [HttpPost]
        public async Task<IActionResult> Create(Data data)
        {
            if (ModelState.IsValid)
            {
                _context.Add(data);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(data);
        }
2
  • 1
    Possible duplicate of Converting DateTime format using razor Commented Aug 13, 2019 at 14:49
  • Could you please explain what do you mean by How would I do this so that all of my model items are effected? Commented Aug 13, 2019 at 15:05

2 Answers 2

1

You can annotate your model with DisplayFormat attributes:

using System.ComponentModel.DataAnnotations;

public class DateModel
{
    [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime StartDate { get; set; }

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

Or if you want to avoid repeating these, you can also use a constant:

using System.ComponentModel.DataAnnotations;

public class DateModel
{
    [DisplayFormat(DataFormatString = Constants.DateOnlyFormat)]
    public DateTime StartDate { get; set; }

    [DisplayFormat(DataFormatString = Constants.DateOnlyFormat)]
    public DateTime EndDate { get; set; }
}

public static class Constants
{
    public const string DateOnlyFormat = "{0:MM/dd/yyyy}";
}
Sign up to request clarification or add additional context in comments.

2 Comments

How would I display only the Date inside of my form?
Which form? There is no such thing in your question, you are only displaying values. But in case you want to also apply it for input fields, here is how that could be done: [DisplayFormat(DataFormatString = DateConstants.DateOnlyFormat, ApplyFormatInEditMode = true)].
0

Use the Date property of DateTime structure.

<td>
      @Html.DisplayFor(modelItem => item.StartDate.Date.ToString("d"))
</td>

For more details refer :

3 Comments

That would have the side effect that the value isn't a DateTime anymore and therefore could be rendered differently in the browser. This would not matter here, but could matter in a form (using @Html.EditFor())
How would I display only the Date inside of my form?
I'd try the DisplayFormat attribute like suggested above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.