Why do my rendered date values appear in this format? "/Date(1517900400000)/"

Why do my rendered date values appear in this format? "/Date(1517900400000)/"

rdmrdm Posts: 194Questions: 55Answers: 4

I'm trying to show date values in "mm-dd-yyyy" format but all my date values look like this "/Date(1517900400000)/".

I didn't see any .NET MVC examples that fit my scenario (or maybe Google didn't index them in the search engine?).

How can I format my date values as "mm/dd/yyyy"?

My controller code is:

 public ActionResult EditorTable(string campus)
    {
        var settings = Properties.Settings.Default;
        var formData = HttpContext.Request.Form;

        using (var db = new Database(settings.DbType, settings.DbAttendanceConnection))
        {
            var response = new Editor(db, "ReconcileSummaryDaysAbsentAndReturned", "Id")
                .Model<ReconcileSummaryDaysAbsentAndReturned>()
                .Field(new Field("BatchDate"))
                .Field(new Field("Reconciled"))
                .Field(new Field("Campus"))
                .Field(new Field("StudentName"))
                .Field(new Field("StudentId"))
                .Field(new Field("EntryDate"))
                .Field(new Field("ExitDate"))
                .Field(new Field("DateAttended"))
                .Field(new Field("DaysAbsent"))
                .Field(new Field("DurationOfAbsence"))
                .Field(new Field("TotalChronicHealthRecordsInSchoolYear"))
                .Field(new Field("SbmsTotalDaysSuspendedInSchoolYear"))
                .Where("Campus",campus)
                .Process(formData)
                .Data();
            return Json(response, JsonRequestBehavior.AllowGet);
        }
    }

My relevant view code is:

 fields: [
                { label: "Batch Date", name: "BatchDate", type: 'datetime', def: function() { return new Date(); }, format: "mm-dd-yyyy" }, /* ...etc */

Column config:

{ data: "BatchDate", className: 'readonly not-allowed' },/* ...etc */

This question has an accepted answers - jump to answer

Answers

  • rdmrdm Posts: 194Questions: 55Answers: 4

    The mystery deepens. Doing this for one date field did the same thing to ALL date fields.

     .Field(new Field("BatchDate").GetFormatter(Format.DateSqlToFormat("mm/dd/yyyy")))
    

    While the tabular data fields rendered in the correct date format, the time component was still included. I need this to be date-only.

  • rdmrdm Posts: 194Questions: 55Answers: 4
    Answer ✓

    Found the answer here: https://datatables.net/forums/discussion/19326/date-time-format

    So I wasn't losing my mind -- just had to render the code differenntly.

  • rdmrdm Posts: 194Questions: 55Answers: 4

    Update: I found a shorter method that turns out to be more reliable.

     columns: [{
          data: "BatchDate",
          className: 'readonly not-allowed',
          render: function (data, type, full) {
          var dtStart = new Date(parseInt(data.substr(6)));
               return dtStart.toLocaleDateString();
               }
          }, // ...etc
    

    In case your datetime is nullable.

     {
          data: "ExitDate",
          className: 'readonly not-allowed',
          render: function (data, type, full) {
          if (data != null) {
               var dtStart = new Date(parseInt(data.substr(6)));
               return dtStart.toLocaleDateString();
          } else {
               return "";
          }     
     }   } // ... etc
    
This discussion has been closed.