Converting the web service DateTime string to the right format in MVC

Posted on 2016-09-09
Last Modified: 2016-09-09
In my MVC Form

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
                                        @Html.DisplayFor(model => model.achPayDate)                                        

in my Model
         [DisplayName("Payment Date")]
        public string achPayDate { get; set; }  

      (achPayDate -- is getting data from the web service)

my achPayDate =      2016-09-21T00:00:00-07:00  
and I wanted to format that to MM/dd/yyyy  so it is 09/21/2016.  

I tried

         [DisplayName("Payment Date")]
        public string achPayDate { get; set; }  

which is not working
and tried

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
                                        @Html.DisplayFor(model => model.achPayDate, "{0:MM/dd/yyyy}")                                        
and is not working

not sure what should I do
Question by:eli411
1 Comment

Accepted Solution

eli411 earned 0 total points
ID: 41792080
Here is what I did and it solved my problem

Found the answer myself!  Here is what I did

in the Model

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

and in the MVC form

                                    <div class="form-group">
                                        @Html.LabelFor(model => model.achPayDate, htmlAttributes: new { @class = "control-label col-md-7" })
                                        @Html.DisplayFor(model => model.achPayDate, new {@class="date"})                                        

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question