How to properly handle db time value using json and javascript / jquery

Does anyone know how to parse a json timespan object? I would like to return the UTC time frame to my view and then convert it to local client time, but I did not find any references to this.

I am using mvc, so I have this model:

public class TimeSpanModel
{
    public TimeSpan StartTime { get; set; }

    public TimeSpanModel()
    {
        this.StartTime = DateTime.UtcNow.TimeOfDay;

    }
}

and in my controller, I return this model to my view as follows:

public ActionResult GetTimeSpanInfo()
    {
        TimeSpanModel tsm= new TimeSpanModel ();
        return Json(tsm);
    }

I make such a call from a view:

$.ajax({
        type: 'POST',
        url: '@Url.Content("~/Controller/GetTimeSpanInfo")',
        success: function (data) {
        alert(data.StartTime);
        var dt = new Date(data.StartTime);
        alert(dt.toString());
        }
    });

but in the first warning window I see only this: [object Object] so I tried to convert the timepan to Date, but in the second warning field I get an invalid date.

, , , "" , , ?

, TimeSpans datetime?

.

P.S. UTCnow, , UTC db - (0). , , - , , , .

+5
2

[object Object], , , TimeSpan JSON-, , .

public ActionResult GetTimeSpanInfo()
    {
        TimeSpanModel tsm= new TimeSpanModel ();
        return Json(tsm.TotalMilliseconds.ToString());
    }

javascript, Date Date () , :

$.ajax({
        type: 'POST',
        url: '@Url.Content("~/Controller/GetTimeSpanInfo")',
        success: function (data) {
        alert(data);
        var dt = new Date(data);
        alert(dt.toString());
        }
    });
+2

:

Javascript

var dateParam = aDate.toUTCString();

#

DateTime aDate = DateTime.ParseExact(dateParam, "r", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None);

#

DateTime formattedDateFromCSharp = aDate.ToString("r");

Javascript

var aDate = Date.parse(formattedDateFromCSharp);
+2

All Articles