Javascript serialization of DateTime in asp.net is not giving a javascript date object?

前端 未结 10 1927
甜味超标
甜味超标 2020-12-01 06:04

When I parse a DateTime to json in .Net it returns a string (i.e. \"\\/Date(1249335194272)\\/\"). How do I make it return a js Date object constructor not wrap

10条回答
  •  醉话见心
    2020-12-01 06:43

    Here's an option using Date.parse and DateTime.ToString:

    var lowerBound = new Date(Date.parse("@Model.LowerBound.ToString("MMMM dd, yyyy")"));
    

    If you need time, consider the following. I believe this relies on a newer javascript spec:

    var lowerBound = new Date(Date.parse("@Model.LowerBound.ToUniversalTime().ToString("s")"));
    

    Here's an option using jQuery:(I'm sure there's a way to add the time here)

    var lowerBound = $.datepicker.parseDate('yy-mm-dd', "@Model.LowerBound.ToString("yyyy-MM-dd")");
    

提交回复
热议问题