0

I've datetimes in sql. But when I display it in cshtml page it shows this:

date:/Date(1432549851367)/

but in database it's :

2015-05-25 14:30:51.367

why?

I have this code:

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();

and that's 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 post it in 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);

                }
            }
        });

it displays message and username normally but not datetime. How can I improve that?

gsiradze
  • 4,583
  • 15
  • 64
  • 111
  • JSON does not provide a specification for a Date. Therefore you need to format it yourself. Either parse the date on the client-side, or else send the date as a string to the client – CodingIntrigue May 25 '15 at 10:47

1 Answers1

1

You must convert date manualy with JS. See how to format date in JavaScript in example below:

var formatedDate = date.getFullYear() + '/' + date.getDate() + '/' date.getDay();
// Output is '2015/5/25'
  • You shouldn't post link-only answers as links can die and your answer will be irrelevant. And you certainly shouldn't post links to [low-quality websites](http://www.w3fools.com/) – CodingIntrigue May 25 '15 at 10:48
  • Please provide an extract from articles you link. Linked only answers may become broken quickly making your answer (that may be valid) useless for future readers (as side note: w3schools is often avoided by many programmers because of his long history of wrong/not complete information). – Adriano Repetti May 25 '15 at 10:49
  • Thanks for recommendations. – Daniel Petrovaliev May 25 '15 at 11:03