Passing the date as a JSON response from a web API to AngularJS

Seeking advice on date formatting.

I am currently developing an AngularJS application that retrieves data from an ASP.NET Web API. Within a table (Model), there is a date field (ThisDate) with the following format:

2015-03-14 12:39:32.470

When I pass this as a response JSON string from ASP.NET -

string json = "{\"CreatedAt\": \"" + Model.ThisDate + "\"}";
response.Content = new StringContent(json, Encoding.UTF8, "application/json")

...the output in my AngularJS app differs to:

14-03-2015 12:39:32

And when I send it as a JSON serialized object from ASP.NET -

JsonConvert.SerializeObject(Model)

...the output in my AngularJS app changes to:

2015-03-14T12:39:32.470

What could be causing this discrepancy? I aim for consistency to ensure correct sorting. Any suggestions on how to accomplish this?

Your insights are greatly appreciated.

Answer №1

I'm still puzzled by the way it converts, but I managed to solve this issue by including a sortable datetime format in the response JSON string like this -

string json = "{\"CreatedAt\": \"" + Model.ThisDate.ToString("s") + "\"}";

Check out more details here

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

The validation directive is run on each individual item within the ng-repeat loop

As I develop a single page application utilizing Angular and Breeze, the challenge of managing entities with dynamic validation arises. With a set of entities displayed on the page using data-ng-repeat, I implement in place validation through toggling betw ...

Retrieving a specific dictionary within a list contained in a JSON document

I have a JSON file that contains annotation data from my dataset, specifically for YOLO object detection. The structure of the JSON file is as follows: { "description": "", "tags": [], "size": { & ...

Implementing dynamic props in Vue2 component by passing arbitrary named variables

Having recently delved into Vue, I am facing a challenge that has left me scratching my head after consulting the documentation: I am struggling to pass an arbitrarily named variable as a prop to a component instance. As per my understanding, props serve ...

Leveraging keyboard input for authentication in Angular

Would it be possible to modify a button so that instead of just clicking on it, users could also enter a secret passphrase on the keyboard to navigate to the next page in Angular? For example, typing "nextpage" would take them to the next page. If you&apo ...

Using Jquery to detect if there are any Space characters in the user input

In my form, users are required to set up a new Username. The problem arises when they include a space in their username, which I want to prevent. Currently, I am able to detect the presence of a space with this code: var hasSpace = $('#usernameValue ...

Setting up NextJS on Vercel for website deployment can encounter a common error known as ENOENT Error, which is caused by the absence of a specific file or

Everything works perfectly on my local machine: import fs from 'fs' import path from 'path' export default function createNewDirectory (tokenSignature: string) { const directoryPath = path.join(process.cwd(), 'notes', to ...

Transmit information between controllers during pageload in AngularJS without utilizing $rootscope

I currently have 2 controllers set up as follows: app.controller('ParentMenuController', function ($scope,MenuService) { $scope.contentLoaded = false; $scope.showButton = false; $scope.showButton = MenuService ...

Unable to refresh changing list of checkboxes

Seeking assistance: How can I refresh the drop-down list on the "Remove Locations" page? The list is populated from localstorage.city. To refresh, click on 'Open Panel' and then select Remove Locations. You will see a list of cities that were ad ...

Is it possible to create a TypeScript generic type that transforms a Record into a type by utilizing the `as const` keyword?

Imagine this scenario: I define const foo = { myKey: 'myValue' } as const Now, when I ask for typeof foo, I get { readonly myKey: 'myValue' } If I have a type MyType = Record<string, string>, and I want to create a modifier (let ...

Why is the Angular directive '=&' value binding to the scope showing as undefined?

An Angular directive has been defined within my application: (function() { 'use strict'; angular .module('almonds') .directive('security', ['$animate', 'AuthFactory', directive]); fun ...

What is the process for establishing the accurate primary key in a SQL database through an HTTP POST request?

I am currently working on a web application where I need to write data to a database using HTTP POST. The database already has existing data and the primary key (PK) is set to ID. My question is, what is the best practice for assigning the correct and un ...

Transform XML to JSON format with Python by leveraging the lxml library along with XSLT operations

Currently, I am facing an issue while trying to convert XML to JSON using python3, lxml-library, and XSLT. Every time I run my code, it throws a XSLTParseError, leaving me confused about the next steps to take. Illustrative XML Snapshot: <SOAP-ENV: ...

Check for the absence of a field in a deserialized object using the JsonConvert class within the Json.NET library (Newtonsoft)

Currently, I am facing an issue while deserializing JSON objects using Json.NET. The problem is that when I deserialize an object without the required properties, instead of throwing an error, it returns default values for those properties. It's cruci ...

Combining various intersection tables and categorizing outcomes into a JSON-styled array

Let's imagine a scenario where we have a table of individuals: +----+------+ | id | name | +----+------+ | 1 | John | | 2 | Mary | | 3 | Jane | +----+------+ In addition, there are multiple tables for different types of clothing, such as a footwea ...

Using JQuery's appendTo method with a lengthy string of elements that includes a mix of single and double quotes: a step-by-step guide

My content script is fetching data from the server in the form of an array of objects. The structure looks something like this: [ { "lang": "English", "videos": [ { "embed": "<iframe width='100%' height='421px&apo ...

Maintaining an object's position steady in relation to the camera in Three.js

I am trying to figure out how to maintain an object's position relative to the camera. Specifically, I want one object to be viewable from all angles using a trackball camera, while another object should always stay in the same position relative to th ...

Does Next.js pre-render every page, or does it only pre-render the initial page?

As I dive into the world of nextjs, I'm coming across conflicting information. Some sources claim that nextjs only prerenders the first page, while others suggest that all pages are prerendered by default. This contradiction has left me confused about ...

Using both CASE and MATCH operators within an array in Neo4j's Cypher Query Language (

Using the code snippet below, I am attempting to retrieve all details related to user data where the checked value is either 1 or 0. I have noticed that 'WHERE flight.checked IN check' does not seem to be properly working. Is it appropriate to u ...

AngularJS: Harnessing the Power of Directives with Template-Defined Functions. Unveiling the Secret to Access

I want to implement custom templates for directives. Most solutions involve using the $compile service within the linker function. However, the directive factory also supports a template function. Why not leverage this option instead? Here's an exam ...

The child controller in the Angular Router UI is having trouble initiating

When I navigate to #/systemadmin/edit/Testing using the route definition below, it displays the SystemAdminController instead of the one specified in the child route. What am I overlooking? $stateProvider.state('systemadmin', { url: '/syst ...