Display Time only in View from DateTime

عبدالمجيد العتيبي 21 Reputation points
2023-05-11T10:24:35.09+00:00

Hello ,

I have a Form that contains two input fields of type Time (Start time , End Time) after submitting the form I want to Show The Information in the View Like this (10:30 AM) I try Display Format but it's not working

or is it better to use String to save the time instead of DateTime (hint : I'm using the Start , End to query something between these time)

Here is My Model

public class Shifts
    {
        public int Id { get; set;}
        [DisplayFormat(DataFormatString = "{dddd, dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
        public DateTime StartTime { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{hh:mm tt}")]
        public DateTime EndTime { get; set; }
       

    }

In the view

    @foreach (var item in Model.ShiftsList)
                                    {
                                        <tr>
                                            <td></td>
                                            <td hidden>@item.Id</td>
                                            <td>@item.Name</td>
                                            <td>
                                                @Html.DisplayFor(x => item.StartTime)
                                            
                                            </td>
                                            <td>
                                                @Html.DisplayFor(x => item.EndTime)


                                            </td>

                                            <td>
                                                <button class="btn btn-info" id="viewbtn">Edit</button>
                                            </td>


                                        </tr>
                                    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,547 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,308 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,385 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,525 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,856 Reputation points
    2023-05-11T13:01:22.1133333+00:00

    I'm not sure what's wrong on your end. The code you've shared works perfectly.

        public class Shifts
        {
            public int Id { get; set; }
    
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:dddd, dd/MM/yyyy HH:mm}", ApplyFormatInEditMode = true)]
            public DateTime StartTime { get; set; }
    
            [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:hh:mm tt}")]
            public DateTime EndTime { get; set; }
        }
    
        public class ShiftVm
        {
            public List<Shifts> ShiftsList { get; set; }
        }
    
            public ActionResult Index()
            {
                ShiftVm vm = PopulateVm();
                return View(vm);
            }
    
            private ShiftVm PopulateVm()
            {
                return new ShiftVm()
                {
                    ShiftsList = new List<Shifts>
                    {
                        new Shifts() { Id = 1, StartTime = DateTime.Now, EndTime = DateTime.Now.AddHours(8) },
                        new Shifts() { Id = 2, StartTime = DateTime.Now.AddHours(24), EndTime = DateTime.Now.AddHours(32) },
                    }
                };
            }
    
    
    @model MvcIdentityDemo.Models.ShiftVm
    @{
        ViewBag.Title = "Home Page";
    }
    
    
    <table class="table">
        @foreach (var item in Model.ShiftsList)
        {
            <tr>
                <td></td>
                <td>
                    @Html.DisplayFor(x => item.StartTime)
    
                </td>
                <td>
                    @Html.DisplayFor(x => item.EndTime)
    
    
                </td>
    
                <td>
                    <button class="btn btn-info" id="viewbtn">Edit</button>
                </td>
    
    
            </tr>
        }
    </table>
    

    Capture


0 additional answers

Sort by: Most helpful