0

I am using jQuery and the datetime values are not showing correctly.

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

enter image description here

Here is the model:

using System;

namespace GbngWebApi2.Models
{
    public class BlogForMaint
    {
       public int BlogId { get; set; }
       public int UserId { get; set; }
       public int BlogCategoryId { get; set; }
       public string BlogTitle { get; set; }
       public bool ActiveSwitch { get; set; }
       public int LikeCount { get; set; }
       public int DisLikeCount { get; set; }

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

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

Here is part of the view related to the dates:

"columns": [
{
  "title": "Actions",
  "data": "BlogId",
  "searchable": false,
  "sortable": false,
  "render": function (data, type, full, meta) {
    return '<a href="@Url.Action("Edit", "BlogMaint")?blogId=' + data + '" class="editBlog">Edit</a> | <a href="@Url.Action("Details", "BlogMaint")?blogId=' + data + '" class="detailsBlog">Details</a> | <a href="@Url.Action("Delete", "BlogMaint")?blogId=' + data + '" class="deleteBlog">Delete</a>';
    }
  },
  { "title": "User", "data": "UserId", "searchable": true },
  { "title": "Blog Title", "data": "BlogTitle", "searchable": true },
  { "title": "Published", "data": "ActiveSwitch", "searchable": true },
  { "title": "Like Count", "data": "LikeCount", "searchable": true },
  { "title": "DisLike Count", "data": "DisLikeCount" },
  { "title": "Modified", "data": "ModifiedDateTime" },
  { "title": "Created", "data": "CreatedDateTime" }
],

This is the data just before it is passed out of the web api to the web client (they look good):

enter image description here

This is how it looks as it gets back to the web client:

I see that the datetime has a T in it. "2020-05-01T13:05:43.103" Is that correct and the problem or is it something else?

entry:
{"Status":1,"BlogForMaintList":[{"BlogId":4,"UserId":1,"BlogCategoryId":1,"BlogTitle":"About","ActiveSwitch":true,"LikeCount":0,"DisLikeCount":0,"ModifiedDateTime":"2020- 
05-01T13:05:43.103","CreatedDateTime":"2020-05-01T13:05:43.103"}, 
{"BlogId":5,"UserId":1,"BlogCategoryId":1,"BlogTitle":"Who I 
am.","ActiveSwitch":true,"LikeCount":0,"DisLikeCount":0,"ModifiedDateTime":"2020-05- 
01T13:05:43.11","CreatedDateTime":"2020-05-01T13:05:43.11"}]}

listValue = {[
  {
    "BlogId": 4,
    "UserId": 1,
    "BlogCategoryId": 1,
    "BlogTitle": "About",
    "ActiveSwitch": true,
    "LikeCount": 0,
    "DisLikeCount": 0,
    "ModifiedDateTime": "2020-05-01T13:05:43.103",
    "Cre...

listofentries :
[
  {
    "BlogId": 4,
    "UserId": 1,
    "BlogCategoryId": 1,
    "BlogTitle": "About",
    "ActiveSwitch": true,
    "LikeCount": 0,
    "DisLikeCount": 0,
    "ModifiedDateTime": "2020-05-01T13:05:43.103",
    "CreatedDateTime": "2020-05-01T13:05:43.103"
  },
  {
   "BlogId": 5,
   "UserId": 1,
   "BlogCategoryId": 1,
   "BlogTitle": "Who I am.",
   "ActiveSwitch": true,
   "LikeCount": 0,
   "DisLikeCount": 0,
   "ModifiedDateTime": "2020-05-01T13:05:43.11",
   "CreatedDateTime": "2020-05-01T13:05:43.11"
   }
]
user3020047
  • 868
  • 1
  • 15
  • 45
  • Have you looked at this? https://datatables.net/plug-ins/dataRender/datetime – Damian70 May 04 '20 at 16:08
  • `/Data(1234)/` is .net's standard (read: shite) way of formatting dates when converting to json. Use a different converter or an ActionFilter (preferred as it then applies automatically). Or use a string in your Model and convert on the server. – freedomn-m May 04 '20 at 16:12
  • freedomn-m, I used an ActionFilter. I used the solution by dav_i. See: https://stackoverflow.com/questions/726334/asp-net-mvc-jsonresult-date-format – user3020047 May 05 '20 at 15:13
  • freedomn-m, how do I give you credit? – user3020047 May 05 '20 at 15:14
  • Damian70, not sure how to add render: $.fn.dataTable.render.moment( 'Do MMM YYYYY' ) to my version above. – user3020047 May 05 '20 at 15:15

0 Answers0