Tips for correcting the `/Date(xxxxxxxxxxxxx)/` formatting issue in asp.net mvc

As a programming novice, I am trying to display data from my database server on the web using a datatable in asp.net mvc. After following a tutorial video on YouTube, I encountered an issue where the date and time columns in my table are displaying as /Date(xxxxxxxxxxxxx)/ instead of the correct format. How can I fix this?

Below is the code snippet from my view page:

 $(document).ready(function () {

           

            $("#AttendanceTable").DataTable(
                {
                    "ajax": {
                        "url": "/Attendance/GetList",
                        "type": "GET",
                      

                    },
                    "columns": [
                        { "data": "PersonInformationId" },
                        { "data": "AttendanceTime" },
                        { "data": "ActionType" },

                    ]
This is the relevant section of my Controller:

        public ActionResult GetList()
        {
            using (DBModel db = new DBModel())
            {
                var AttList = db.Attendances.ToList<Attendance>();
            
                return Json(new { data = AttList }, JsonRequestBehavior.AllowGet);
               
            }

I have come across suggestions to convert the JSON date to a normal date format in JavaScript, but I am unsure how to do this.

Answer №1

Success at last! I cracked it by using this code, tailored for date and time format [MM/dd/YYYY HH:mm am:pm]

function (jsonDate) {
                                var d = new Date(parseInt(jsonDate.substr(6)));
                                var m, day;
                                m = d.getMonth() + 1;
                                if (m < 10)
                                    m = '0' + m
                                if (d.getDate() < 10)
                                    day = '0' + d.getDate()
                                else
                                    day = d.getDate();
                                var formattedDate = m + "/" + day + "/" + d.getFullYear();
                                var hours = (d.getHours() < 10) ? "0" + d.getHours() : d.getHours();
                                var dayMode = hours < 12 ? "am" : "pm"; 
                                var hours12 = hours <= 12 ? (hours == 0 ? 12 : hours) : hours- 12;
                                var minutes = (d.getMinutes() < 10) ? "0" + d.getMinutes() : d.getMinutes();
                                var formattedTime = hours12 + ":" + minutes + dayMode;
                                formattedDate = formattedDate + " " + formattedTime;
                                return formattedDate;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to running examples of three.js on a local web browser

Currently, I am attempting to run the examples from three.js locally in a web browser on MacOS. To do this, I have cloned the entire three.js repository and attempted to open a file in a browser (such as three.js/examples/misc_controls_orbit.html). However ...

Utilizing JavaScript for the removal or hiding of span elements with specific class attributes

I am currently working on a software project that involves compiling HTML fragments and then exporting them to Microsoft Word. My goal is to create a script that will cycle through these compiled fragments and remove specific tags that have a particular CS ...

"Creating multiple circles on an HTML5 canvas using an iPad: A step-by-step guide

My current code is only drawing one circle at a time on an iPad view, but I want to be able to draw multiple circles simultaneously. How can I achieve this? // Setting up touch events for drawing circles var canvas = document.getElementById('pain ...

How can I extract just the initial 2 letters of a country name using AmCharts maps?

Having trouble with Amcharts maps. I have a map that displays countries as United States, but I only want them to show as US. Is there a country formatter available for this issue? Any help is appreciated. ...

Exploring the properties and methods of a node.js module object through iteration

Looking to compile a comprehensive list of all the Properties and Methods associated with the os Node.js module. One possible method is: var os = require('os'); Object.keys(os); Object.getOwnPropertyNames(os); Given that the os module is an Obj ...

Singleton constructor running repeatedly in NextJS 13 middleware

I'm encountering an issue with a simple singleton called Paths: export default class Paths { private static _instance: Paths; private constructor() { console.log('paths constructor'); } public static get Instance() { consol ...

Steps to ensure my collapsible is open from the start

Is there a way to have the collapsible content open by default without much hassle? I'm a bit lost here and could use some guidance. I must confess, I'm not very familiar with this, so hopefully, it's not too complicated. While my question ...

How can I display JSON data as key-value pairs in ReactJS?

Transitioning from JavaScript to React, I've come across some threads that touch on this topic but none quite hit the mark. I have a local JSON file that was created with a Python script, and it looks something like this: [{"hello": 10, "world": 15 ...

Save log discrepancies to a document

Upon completing my website for graduation, there is still one important feature I want to add. I would like to implement a script that sends the name of the website where it is installed, as well as any error messages, to my website. For instance: The fol ...

How to extract the final element from a JSON array using Volley?

package net.myapp.app.weatherapplication; import android.os.Handler; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import com.android.volley.Request; import com.android.volley.RequestQueue; imp ...

Suggestions for displaying combination data in JSON format?

Currently, I am working on integrating an API that provides information on the number of users using a specific app. For instance, let's say I need to retrieve data indicating 10 individuals are exclusively using App1, 8 are solely using App2, 8 are ...

Execute the function only in response to changes in the data

I have a scenario where I am making an ajax call every 3 seconds to keep my app updated with rapidly changing information. However, the expensive function that I run inside the $.done() callback is causing my app to slow down. I want to optimize this proce ...

Running a Python script through a Javascript event

I'm looking to enhance my webpage by implementing a feature where users can generate a personalized QR code with the click of a button. My current setup involves a Python script that creates the QR code image and saves it as a .png file. I want to int ...

What is the best way to combine objects that share the same id?

View Image: Displaying Image POST ID's https://i.stack.imgur.com/JO5OF.png I attempted to resolve this using jQuery each, but I am uncertain about the next steps. Check out my Code: jQuery(document).ready(function($) { var data, exampleData, , ...

Using AngularJs, you can access the document.body.onfocus event within the controller of

I am attempting to detect when the user closes or cancels the File Upload Window <input type="file"> Since there isn't a built-in listener for the close event of the file upload, I am trying to capture it using the document.body.focus event, s ...

What sets apart these two JavaScript namespaces?

My main goal is to expertly organize my javascript code by eliminating any global elements. I came across two namespace declaration methods in this informative post, and now I'm looking for your input on the advantages and disadvantages of each. ...

Unable to generate a new file within the directory C:inetpubwwwroot through automated means

While working on an ASP.NET webpage, I encountered an issue with a function in the code behind that creates and opens a file using a JavaScript command. The function works perfectly within the IDE - creating files, allowing me to save them, etc. However, o ...

Formatting dates in AngularJS

Is there a way to customize the date format within an ng-repeat loop? I attempted to format it as shown below <div class="col-date">{{row.Calendar.start | date : "dd.MM.y"}}</div> However, it did not produce the desired outcome. What is th ...

issues with jquery's .each() method not functioning properly

I am looking to iterate over each item in lists and then each list within lists to calculate the number of items in list if the number of items in list is 3, I want to display an alert with the value of each item JSON Data { "lists":[ { ...

What adjustments can I make to my smooth-scrolling JavaScript code in order to exclude my Bootstrap Carousel from the scrolling

On my website, I have incorporated JavaScript to create a smooth-scrolling effect when a user clicks on a hyperlink. This feature is essential as the landing page includes a chevron-down icon that prompts the user to navigate to the next section of the pag ...