Enhancing Date formatting in Jquery Data tables following ASP.NET Serialization

Currently, I am facing an issue with formatting dates in a SQL database query that is being serialized by ASP and then converted to JSON for display in Datatables using JavaScript. Instead of the correct date format, I am seeing: /Date(1424563200000)/.

I have attempted to rectify this problem by implementing the following code snippet:

function ToJavaScriptDate(value) {
    var pattern = /Date\(([^)]+)\)/;
    var results = pattern.exec(value);
    var dt = new Date(parseFloat(results[1]));
    return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

However, I am uncertain how to invoke this function every time my Datatable attempts to read a date.

The configuration of my table is as follows:

$('#YourTaskTable').dataTable({
    "ajax": "App_JSON/YourTaskTable.txt",
    "columns": [
        { "data": "TName" },
        { "data": "RegistrationNo" },
        { "data":  "DueDate"}
    ]
});

I have tried modifying it like this:

$('#YourTaskTable').dataTable({
    "ajax": "App_JSON/YourTaskTable.txt",
    "columns": [
        { "data": "TName" },
        { "data": "RegistrationNo" },
        { "data": ToJavaScriptDate("DueDate")} //Function call added here  
    ]
});

Unfortunately, this approach does not seem to be effective as I am still unable to see the formatted date. How can I properly utilize this function to convert the date in this scenario?

Answer №1

It is recommended to utilize the mRender method in this scenario. Remember to make use of aoColumns instead of columns, and extract your data from full[], which serves as the datasource for the corresponding row.

"aoColumns": [
{
    'mRender': function(data, type, full) {
       return ConvertToJavaScriptDate(full[2])
    }
},
etc...

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

Trouble with hide/show loop in setTimeout function

I have a special animation with 3 text items that are initially invisible. The goal is to make these items appear one by one with a delay of 2 seconds after clicking a button. Each item should be visible for 1 second before fading out and making way for th ...

Exploring the Intersection of Windows 8 Store Applications and jQuery: Leveraging MSApp.execUnsafeLocalFunction

Developing a Windows 8 JavaScript Store App (using Cordova) has led to some complications when using jQuery. It seems that in order to utilize certain functions, I have had to modify the jQuery library by adding: MSApp.execUnsafeLocalFunction While this ...

Retrieve an array of wide characters leading to a memory leak

I need help creating a JSON string from my input arrays. I used "new" to allocate memory for the JSON, but I'm unsure about how or where to deallocate this memory. Is there a more efficient way to write this function? wchar_t* SetExpectedTabsDat ...

Speed up - Handle alias resolution with module federation

Currently import federation from '@originjs/vite-plugin-federation'; import react from '@vitejs/plugin-react-swc'; import dns from 'dns'; import path from 'path'; import { visualizer } from 'rollup-plugin-visual ...

Swipe to modify Array

Currently, I am in the process of developing an application that features a Swipe card interface using both AngularJS and the Ionic framework. The functionality of this app will be similar to the one found at . When swiping to accept a card, I want the ar ...

Encountering a mistake due to the anticipated atom not being found at the specified

In my react application, I am encountering an issue with allowing foreign characters along with English in the input field of a form. I have implemented a regular expression as follows: const alphabetRegex = /^([A-Za-z]+ )+[A-Za-z]+$|^[A-Za-z]*\p{L}/g ...

Discovering the smallest, largest, and average values across all properties in an array of objects

Given an array of objects with varying values, the task is to determine the minimum, maximum, and average of the properties in that array. For example, consider the following array: const array = [{ "a": "-0.06", "b": "0.25", "c": "-0.96", ...

validating if Object may be either 'null' or 'undefined'

In the code snippet below, I am attempting to verify whether hostel.country.address is null: return hostel.country.address && hostel.country.address.internalEmployeeIdentifier !== null || hostel.country.address.exter ...

AngularJS enables developers to make cross-domain requests using the $http service

Working on a simple AngularJS website that makes API calls for JSON data. Running into issues with Cross-Origin Resource Sharing (CORS) - any suggestions on how to handle cross-domain requests? Error: Issue: XMLHttpRequest cannot load . The 'Access ...

The AngularJS HTTP interceptor is a crucial component for handling

Is there a way to use an interceptor in AngularJS to log "finished AJAX request" when any request is completed? I've been exploring interceptors and currently have the following setup, but it triggers at the beginning of the request rather than the e ...

What might be the reason for jQuery not functioning in Internet Explorer 11?

Working on developing a slideout menu for my website using jQuery. It functions perfectly in Chrome, but encountering issues in Internet Explorer (IE11). Extensive search hasn't provided a solution yet. Seeking assistance and any help would be highly ...

Solving the problem of 'json mime-type on 2003 server

I am currently facing an issue: On a 2003 server with iis 6, I have a functioning solution. However, during every deployment of the solution, I find myself having to manually configure the MIME type on the iis. Although I have implemented this in my web ...

WebDriverIO effortlessly converts the text extracted using the getText() command

One of my webpage elements contains the following text: <span class="mat-button-wrapper">Sicherheitsfrage ändern</span> However, when I attempt to verify this text using webdriver, it indicates that it is incorrect assert.strictEqual($(.mat ...

Substitute missing values in a dictionary

My current script retrieves information on financial instruments (ETF's) from an API: import xlwings as xw import requests # Open Excel file and reference ETF names and countries worksheet: excel_file = xw.Book("""FinMkt.xlsm"&quo ...

What are the recommended callbacks for initiating ajax calls and managing files/chunks during dropzone chunk uploading?

I am currently working on implementing chunk uploading using dropzone js and php. My main concern is regarding the placement of ajax calls in this process. When dealing with single file uploads, specifying the URL parameter is sufficient. However, when ...

The callback function for ajax completion fails to execute

My current framework of choice is Django. I find myself faced with the following code snippet: var done_cancel_order = function(res, status) { alert("xpto"); }; var cancel_order = function() { data = {}; var args = { type:"GET", url:"/exch ...

Creating Typescript types based on the values of other props: A guide

Can the TypeScript prop type be dynamically changed based on the runtime value of another prop? For instance type MyComponent = { propA: boolean | string propB: typeof propA boolean ? number : string } Is it feasible to determine the prop type of p ...

Tips for getting the setTimeout() function to behave like a for loop

Is there a way to achieve the setTimeout() function's behavior in a for-loop? Take a look at this code snippet: function hello() { for (let index = 0; index < 3; index++) { setTimeout(function () { console.log('What&bs ...

Ajax undoes any modifications enacted by JavaScript

When using ajax, I trigger an OnTextChangedEvent. Before this event occurs, there is a Javascript function that validates the input field and displays text based on its validity. However, once the Ajax is executed, it resets any changes made by the Javascr ...

Failure to trigger AJAX Success or Error Events

I'm struggling to understand why this code isn't working or find any relevant resources. When I check the json object in Firebug, it either returns success: false or success: true from the post request, so I'm confused as to why the function ...