show the date in the appropriate format

My SQL database stores datetimes, but when I try to display them on a cshtml page, it shows this format:

date:/Date(1432549851367)/

However, in the actual database, the datetime is presented as:

2015-05-25 14:30:51.367

Why is there a difference?

This is the code I have:

var conv = (from c in db.Conversations
                                where (c.FirstUser == user.Id && c.AdminUser == id)
                                || (c.FirstUser == id && c.AdminUser == user.Id)
                                select new
                                {
                                    Message = from m in c.Messages
                                              select new MessageData()
                                              {
                                                  Message = m.Message1,
                                                  SentTime = m.MessageTime,
                                                  From = m.User.UserName
                                              }
                                }).FirstOrDefault();

This is my model class:

 public class MessageData
     {
             public string From { get; set; }
             public string Message { get; set; }
             public DateTime? SentTime { get; set; }
     }

And here's how I render it on the page:

function refresh(id) {
        $.getJSON("../Chat/ViewConversation/" + id, function (data) {
            $('#ToId').val(id);



            $('#conversations').html('');
            if (data[0].Message1 == 'no message') {
                $('#conversations').append('no message');
            } else {
                for (var i = 0; i < data.length; i++) {

                    console.log(data[i].SentTime);

                    var ctx = '<div class="from">' + data[i].From + '</div>' +
                    '<div class="messageBody">' + data[i].Message + '</div>' +
                    '<div class="date">date:' + data[i].SentTime + '</div><hr/>';
                    $('#conversations').append(ctx);

                }
            }
        });

The message and username are displayed correctly, but the datetime is not. How can I fix this issue?

Answer №1

To convert a date manually with JS, you can follow the example below on how to format date in JavaScript:

var formatedDate = date.getFullYear() + '/' + date.getDate() + '/' date.getDay();
// This will output '2015/5/25'

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

I am looking to have my page refresh just one time

I have created an admin page where I can delete users, but each time I delete a user, the page requires a refresh. I attempted to use header refresh, but this action caused my page to refresh multiple times. Is there a way to ensure that my page only refr ...

Create buttons to increase or decrease values using Bootstrap and JavaScript

What is the proper way to make this button function properly? I tested adding this line of javascript code, however it prompts an alert without clicking on the buttons as well. document.querySelector(".btnminus").addEventListener(onclick,ale ...

Executing a function in a different AngularJS controller: a comprehensive guide

I've been scouring the internet for information on setting up two controllers in my AngularJS module, but all the answers I find seem to be outdated. I want to have one controller handling $http GET requests and another displaying success or error mes ...

How to update an object in an array within a collection using ExpressJS and MongoDB

I'm having trouble updating an array within a collection. I can't seem to find the object in the array and add new values to it. I've tried a few methods, but it looks like I can't use collection methods on arrays? router.post('/m ...

Struggling with the conundrum of aligning a constantly changing element amid the

I was optimistic about the code I wrote, hoping it would work out in the end. However, it seems that my expectations might not be met. Allow me to provide some context before I pose my question. The animation I have created involves an SVG element resembl ...

What is the most efficient way to read multiple JSON values from a file or stream in Python without putting in too much effort

Is there a way to efficiently read multiple JSON objects from a file or stream in Python, one at a time? The built-in json.load() method reads until the end of the file and does not allow for reading a single object or iteratively over objects. Ideally, I ...

Creating a new role with RoleManager ApplicationRole

Recently, I developed a method to add new roles in a more efficient way by utilizing the following code snippet: public async Task<IActionResult> CreateRole(ApplicationRoleModel model) { try { foreac ...

How can one access a Kafka Ksql Push query from a .NET platform?

Currently, I am dealing with a high volume of Kafka messages in .NET using a Kafka consumer. The first step in my process involves parsing the JSON and removing many messages based on a specific field value in the JSON. I want to avoid processing (and do ...

Is there a way to retrieve the precise floating-point width of an element?

When attempting to determine the precise width of a table cell, I encountered some discrepancies. While IE9's developer toolbar indicated a width of 203.68px in the Layout tab, using element.clientWidth and other methods yielded a rounded integer valu ...

Securing the transfer of information between C# and MySQL by encrypting data

For a security audit of the system at my workplace, I need to encrypt all communication between my C# windows application and the MySQL database it uses to store data. Currently, both the MySQL DB and the windows application are hosted on the same machin ...

exploring methods to prevent flash of unstyled content (fouc)

Looking for a way to control CSS with a cookie: Want the user's style choice to stick until they change it or the cookie expires Avoid any default styling issues when the user returns Avoid using jquery, libraries, or frameworks Need compatibility w ...

An issue arose in ReactJS when trying to utilize the GraphQL API

Struggling to incorporate the graphql API into a react js application. Uncertain if this approach is correct. import React, { Component } from 'react' import "./styles.css"; import axios from 'axios'; const appBaseUrl = axios.create({ ...

Using TypeScript to create a list of key-value pairs, which will be converted into a list of objects

Is there a more elegant way to transform an array of key-value pairs into a list of objects in TypeScript? let keys : string [] = ["name", "addr", "age"]; let values : string [][] = [["sam", "NY", "30"],["chris", "WY", "22"],["sue", "TX", "55"]]; The desi ...

Utilizing Jackson in Java to parse a JSON file and sequentially inputting data into a singular object

My goal is to convert a large JSON/JSON-LD file into XML format. The JSON file consists of various events, each with different information/data. Instead of creating separate objects for each event, I want to read them one by one and store the data in a sin ...

I encountered an issue stating, "The function `req.redirect` is not recognized."

Recently starting out with node development. Encountering the error below: TypeError: req.redirect is not a function at Post.create (/var/www/html/node_blog/index.js:40:7) at /var/www/html/node_blog/node_modules/mongoose/lib/utils.js:276:16 a ...

I am looking for a way to retrieve results using PHP's fetch assoc method

// Password Retrieval Request $app->get('/api/admin/forgot', function(Request $request, Response $response){ $email = $request->getParam('email'); $sql = "SELECT admin_password FROM administrators WHERE admin_email = '$emai ...

This basic Javascript script is not functioning properly at the moment

I have encountered an issue with a basic javascript code that I wrote. Although it is a simple code, I am unable to identify any errors. Please review and let me know if you spot any mistakes. <div> <p onclick="closePopup()" id="adve">Someth ...

Building a Stack container for an unknown data type

In my code, I have a class called Foo defined as follows: class Foo { public int id{get;set;} public IEnumerable<Foo> Childs; //some other properties } My goal is to apply certain business logic to a Foo object and all of its children ...

What steps can I take to persistently subscribe to SignalR from an Angular service even in the event of connection failures?

Is there a way to safely attempt to connect to SignalR with intervals between attempts until the connection is established? Also, does anyone have advice on how to handle the different stages of connectivity to the web sockets effectively? We are utilizin ...

Managing the response from a variable returned by jQuery's .load method

I am facing an issue with my code. I am using jQuery post to validate whether the name of the filter is available for use. $.post('check-username.php', $("#myform").serialize(), function(data) { alert(data); }); When I use the alert to disp ...