ASP.NET MVC JsonResult Date Format

前端 未结 25 3261
渐次进展
渐次进展 2020-11-21 11:22

I have a controller action that effectively simply returns a JsonResult of my model. So, in my method I have something like the following:

return new JsonRes         


        
25条回答
  •  日久生厌
    2020-11-21 11:50

    Not the most elegant way but this worked for me:

    var ms = date.substring(6, date.length - 2);
    var newDate = formatDate(ms);
    
    
    function formatDate(ms) {
    
        var date = new Date(parseInt(ms));
        var hour = date.getHours();
        var mins = date.getMinutes() + '';
        var time = "AM";
    
        // find time 
        if (hour >= 12) {
            time = "PM";
        }
        // fix hours format
        if (hour > 12) {
            hour -= 12;
        }
        else if (hour == 0) {
            hour = 12;
        }
        // fix minutes format
        if (mins.length == 1) {
            mins = "0" + mins;
        }
        // return formatted date time string
        return date.getMonth() + 1 + "/" + date.getDate() + "/" + date.getFullYear() + " " + hour + ":" + mins + " " + time;
    }
    

提交回复
热议问题