1

I'm getting the date values from data base and try to display it in view but problem is it is not display as a Date. it shows like this /Date(1328207400000)/ when i tried to load 2012-02-03 00:00:00.000. i'm using asp.net MVC 3

View

<tbody id="Activities">

                </tbody>

Script

function GetFilteredActivities()
{
    var selectedActivityId = $('#SelectedActionId').val();

    $.getJSON('@Url.Action("GetFilteredActivityLogs", "Document")', { actionTypeId: selectedActivityId }, function (FilteredActivities)
    {
        var ActivitySelect = $('#Activities');
        ActivitySelect.empty();
        $.each(FilteredActivities, function (index, activity)
        {
            ActivitySelect.append("<tr><td>" + activity.EmployeeNo + "</td><td>" + activity.EmployeeName + "</td><td>" + activity.Description + "</td><td>" + activity.DateCreated + "</td></tr>");
        });
    });
}
Krishan
  • 2,356
  • 6
  • 36
  • 47
  • This link should fix your problem : http://stackoverflow.com/questions/206384/how-to-format-a-json-date – Siva Gopal Jul 23 '12 at 12:23
  • siva - yes, re the json date, that will work well too. I won't update my answer to include as i feel you should add this as a fully formed answer too – jim tollan Jul 23 '12 at 13:19

3 Answers3

0

This date is in the Unix epoch format. The number you obtained is the number of seconds elapsed since 1st January 1970. You should convert it to get back a normal date as output.

You can use this tool to check the value of the date.

RRikesh
  • 14,112
  • 5
  • 49
  • 70
0

as rrikesh says, it's a unix date [number of seconds elapsed since 1st January 1970], try converting it to a c# DateTime object along the lines of:

public DateTime FromUnixEpoch(long unixSeconds)
{
    // create a date that is equal to 1st Jan 1970 (midnight)
    var convertedDate = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    // add the unixSeconds onto the base date and return
    return convertedDate.AddSeconds(unixSeconds);
}

and of course, you have the reverse case of converting a c# DateTime to a unix time:

public double ToUnixEpoch(DateTime sourceDate)
{
    DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0).ToUniversalTime();
    TimeSpan span = (sourceDate.ToUniversalTime() - unixEpoch);
    return span.TotalSeconds;
}

[edit] - in your case above [Date(1328207400000)] it appears to be being saved in milliseconds, in which case, you'll need to factor this into the method (divide by 1000 to convert to seconds). also, i don't THINK your base date is being saved as UTC as the correct valuse for the date above is actually 02/02/2012 18:30 (would i be correct in saying that you are 5:30hrs ahead of GMT?? i.e. based in India perhaps??).

anyway, I'll get my nose out of things now :-)

jim tollan
  • 22,305
  • 4
  • 49
  • 63
0

I did like this.it worked for me.. thanks all who share their experiences. thanks all...

var DateCreated = eval((activity.DateCreated).replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
                var date = DateCreated.toDateString();
                var time = DateCreated.toLocaleTimeString();
                var dateTime = (date+" "+time);
Krishan
  • 2,356
  • 6
  • 36
  • 47