Convert Json date to “Date” in javascript

后端 未结 3 798
轻奢々
轻奢々 2021-01-24 11:18

I am using Visual Studio 2013 with C#.

I am calling one ActionResult method that returns the list of data from Ajax.

The problem here is I am getting date as

3条回答
  •  灰色年华
    2021-01-24 11:32

    You could use a function which return a date if the date string is in the wanted form or the value itself.

    function getDateIfDate(d) {
        var m = d.match(/\/Date\((\d+)\)\//);
        return m ? (new Date(+m[1])).toLocaleDateString('en-US', {month: '2-digit', day: '2-digit', year: 'numeric'}) : d;
    }
    
    console.log(getDateIfDate("/Date(1460008501597)/"));
    console.log(getDateIfDate('abc'));

提交回复
热议问题