2015-10-15 79 views
-2

我有一個ASP.net應用程序,它使用AngularJS來顯示數據。 我正在檢索包含日期列的數據列表。當我使用console.log打印從服務器/數據庫返回的數據時,我看到格式爲「Thu Feb 11 2016 00:00:00 GMT + 1100(AUS Eastern Daylight Time)」的日期。 爲什麼服務器以這種格式返回日期?我該如何去改變日期格式?我想,格式爲DD/MM/YYYY HH:MM 下面是我的一些代碼檢索數據:ASP.net到AngularJS日期格式

public JsonResult GetEventList() 
{ 
var result = from e in db.events 
       select new 
       { 
           event_id = e.event_id, 
           event_name = e.event_name, 
           event_date = e.event_date 
       } 
return Json(result.ToList(), JsonRequestBehavior.AllowGet); 
} 

JSON響應的樣子:

{ event_id: 1, event_name: 'Event', event_date: 'Mon Jan 11 2016 00:00:00 GMT+1100 (AUS Eastern Daylight Time)' } 
+0

你如何從ASP.NET返回日期信息到客戶端? – Dai

+0

「我有一個ASP.NET」...一個ASP.NET *什麼*? – Dai

+0

GetEventList()返回一個json。你能在你的問題中顯示json響應嗎? –

回答

1

其實你得到:

Mon Jan 11 2016 00:00:00 GMT+1100 (AUS Eastern Daylight Time)

你可以嘗試添加自定義格式的Date對象,像這樣:

Date.prototype.formatMMDDYYYYHHMMSS = function() { 
    return (this.getDate()) + 
     "/" + ((this.getMonth() + 1) < 10 ? ("0" + (this.getMonth() + 1)) : (this.getMonth() + 1)) + 
     "/" + this.getFullYear() + 
     " " + this.getHours() + 
     ":" + this.getMinutes() + 
     ":" + this.getSeconds(); 
}; 

如何使用此功能:

用長日期字符串設置日期變量。

var date = "Mon Jan 11 2016 00:00:00 GMT+1100 (AUS Eastern Daylight Time)"; 

將日期轉換爲Date()

date = new Date(date); 

然後,通過使用date.formatMMDDYYYYHHMMSS(),您將獲得轉換日期。

Demo with Javascript

var date = "Mon Jan 11 2016 00:00:00 GMT+1100 (AUS Eastern Daylight Time)"; 
 

 
Date.prototype.formatMMDDYYYYHHMM = function() { 
 
    return (this.getDate()) + 
 
    "/" + ((this.getMonth() + 1) < 10 ? ("0" + (this.getMonth() + 1)) : (this.getMonth() + 1)) + 
 
    "/" + this.getFullYear() + 
 
    " " + this.getHours() + 
 
    ":" + this.getMinutes(); 
 
}; 
 

 
var datea = new Date(date); 
 
console.log(datea.formatMMDDYYYYHHMM());

使用AngularJS一個$過濾器:

(function() { 
 
    "use strict"; 
 
    var app = angular.module("myApp", []); 
 

 
    app.controller("Controller", ["$scope", "$filter", 
 
    function($scope, $filter) { 
 

 
     $scope.eventList = [{ 
 
     "event_id": 1, 
 
     "event_name": "Event", 
 
     "event_date": "Mon Jan 11 2016 00:00:00 GMT+1100 (AUS Eastern Daylight Time)" 
 
     }, { 
 
     "event_id": 2, 
 
     "event_name": "Event 2", 
 
     "event_date": "Mon Jan 11 2016 15:48:00 GMT+1100 (AUS Eastern Daylight Time)" 
 
     }]; 
 

 
     // Using the toDateTime $filter in the Controller. 
 
     $scope.showDate = function(event) { 
 
     console.log($filter("toDateTime")(event.event_date)); 
 
     }; 
 
    } 
 
    ]); 
 

 
    app.filter("toDateTime", function() { 
 
    return function(x) { 
 

 
     Date.prototype.formatMMDDYYYYHHMM = function() { 
 
     return (this.getDate()) + 
 
      "/" + ((this.getMonth() + 1) < 10 ? ("0" + (this.getMonth() + 1)) : (this.getMonth() + 1)) + 
 
      "/" + this.getFullYear() + 
 
      " " + this.getHours() + 
 
      ":" + this.getMinutes(); 
 
     } 
 

 
     return new Date(x).formatMMDDYYYYHHMM(); 
 
    }; 
 
    }); 
 
})();
<html data-ng-app="myApp"> 
 

 
<head> 
 
    <title>Demo AngularJS</title> 
 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> 
 
</head> 
 

 
<body data-ng-controller="Controller"> 
 
    <table> 
 
    <thead> 
 
     <tr> 
 
     <th> 
 
      event_id 
 
     </th> 
 
     <th> 
 
      event_name 
 
     </th> 
 
     <th> 
 
      event_date 
 
     </th> 
 
     <th> 
 
      Show Date 
 
     </th> 
 
     </tr> 
 
    </thead> 
 
    <tbody> 
 
     <tr data-ng-repeat="event in eventList"> 
 
     <td>{{event.event_id}}</td> 
 
     <td>{{event.event_name}}</td> 
 
     <td>{{event.event_date | toDateTime}}</td> 
 
     <!-- Using the toDateTime $filter in the View. --> 
 
     <td> 
 
      <button data-ng-click="showDate(event)" type="button">Show Date</button> 
 
     </td> 
 
     </tr> 
 
    </tbody> 
 
    </table> 
 
</body </html>

0

假設你的時間從ASP側是在DateTime格式,

在控制器檢索從ASP: 「/日期(1441164289937)/」

要過濾(在控制器側):

.filter('convertDateTime', function($filter) { 

    return function(input) { 

     var timeFormat = 'dd MMM yyyy HH:mm'; //date format 

     if(input == null) { return "";} 

     return $filter('date')(parseInt(input.substr(6)), timeFormat); 

    }; 

}) 

你可以修改上面的以下javascript代碼到你喜歡的地方。但是邏輯是一樣的。但是你需要先將你的DateTime改爲javascript日期。那裏有很多例子。希望有所幫助。