Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
471 views
in Technique[技术] by (71.8m points)

asp.net mvc - Date Format from model is not working correctly and is Displaying seconds

I'm trying to have a create a date format that displays the MM/DD/YYYY and time. So basically it's I'm trying to display 11/17/2020 9:36AM but my code ends up displaying mm/dd/yyyy --:-- -- when I use the date format [DisplayFormat(DataFormatString = "{0:g}", ApplyFormatInEditMode = true)] within my model. I want to remove the seconds off the date format

When I go to check on dev tools it says The specified value "11/17/2020 9:36 AM" does not conform to the required format. The format is "yyyy-MM-ddThh:mm" followed by optional ":ss" or ":ss.SSS". I don't know if there's something wrong with my display format in my model or if there a problem within my razor. If it's neither then what could be the problem and what is the solution?

Model

[Display(Name = "Advocate Time Called")]
[DisplayFormat(DataFormatString = "{0:g}", ApplyFormatInEditMode = true)]
public DateTime? AdvocateTimeCalled { get; set; }

View

     <div class="row">
        <div class="col form-group">
        <p class="card-text m-2">
        @Html.LabelFor(model => model.AdvocateTimeCalled)
        </p>
          @Html.EditorFor(model => model.AdvocateTimeCalled, new { HtmlAttributes = new { @class = "form-control"} })
          @Html.ValidationMessageFor(model => model.AdvocateTimeCalled, "", new { @class = "text-danger" })
    </div>
   </div>

enter image description here enter image description here

question from:https://stackoverflow.com/questions/65878984/date-format-from-model-is-not-working-correctly-and-is-displaying-seconds

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Review this site's examples: https://www.csharp-examples.net/string-format-datetime/ I don't think you are wanting the AM/PM designator to be included, but using the {} formatting tag outputs the DateTime to something like "3/9/2008 4:05:07 PM".

It may be that you need to use some other code to format the output value, or a combination of them.

I know I didn't provide a cut-n-paste answer, but I hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...