嗨,我想知道如何轉換JSON字符串日期來自格式響應像「8/24/2016」。我做了一個dateFilter.js位,它沒有按預期工作,所以這是我的嘗試。如何將JSON字符串日期轉換爲自定義格式日期?
這裏的dateFilter.js(實際上它沒有工作錯誤:數據遞歸)
(function() {
angular
.module('myapp')
.filter('date', function ($filter) {
return function (input) {
if (input == null) {
return "";
}
var _date = $filter('date')(new Date(input), 'dd/MM/yyyy');
return _date.toUpperCase();
};
});
})();
以下是我得到JSON與服務(的代碼是不完整的,因爲我想說明我怎麼得到響應。)
function GetEmpDetails(successcallback, failcallback) {
var req = {
method: 'GET',
url: 'http://localhost:2222/api/GetEmployees/GetEmployeeDetails',
headers: {
'Content-Type': 'application/json'
}
}
$http(req).then(successcallback, failcallback);
}
的controller.js
(function initController() {
EmployeeService.GetEmpDetails(function (res) {
$scope.employeeDetails = JSON.parse(res.data);
//console.log(res.data);
}
});
最後將過濾器應用於html。
<table id="basic-datatables" class="table table-striped table-bordered" cellspacing="0" width="100">
<thead style="text-align:match-parent">
<tr>
<th rowspan="1" colspan="1" style="width:195px">First Name</th>
<th rowspan="1" colspan="1" style="width:195px">Last Name</th>
<th rowspan="1" colspan="1" style="width:200px">Date Of Birth</th>
<th rowspan="1" colspan="1" style="width:100px">Gender</th>
<th rowspan="1" colspan="1" style="width:200px">Email</th>
<th rowspan="1" colspan="1" style="width:100px">Mobile</th>
<th rowspan="1" colspan="1" style="width:190px">Designation</th>
<th rowspan="1" colspan="1" style="width:200px">Date of Join</th>
<th rowspan="1" colspan="1" style="width:195px">NIC</th>
<th rowspan="1" colspan="1" style="width:100px">Dept. Name</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="emp in employeeDetails.slice(((currentPage-1)*itemsPerPage),((currentPage)*itemsPerPage))" style="text-align:center">
<td>{{emp.fname}}</td>
<td>{{emp.lname}}</td>
<td>{{emp.DOB | date}}</td> //applying the filter
<td>{{emp.gender}}</td>
<td>{{emp.email}}</td>
<td>{{emp.mobile_no}}</td>
<td>{{emp.designation}}</td>
<td>{{emp.date_of_join | date}}</td> //applying the filter
<td>{{emp.nic}}</td>
<td>{{emp.department_name}}</td>
</tr>
</tbody>
</table>
那麼,我會如何做到這一點是有任何其他方式的轉換。
Final note: right now without the filter: 7/25/2016 12:00:00 AM want to convert to 7/25/2016
幫助,將不勝感激。
你有沒有嘗試{{EMP。DOB | date:'MM/dd/yyyy'}} –
您可能想要清除所有不相關的代碼並只保留所需的內容。 –
這不是「JSON字符串日期」,而只是一個字符串。 JSON不會聲明任何日期數據類型。 – zerkms