嗨,我想知道如何将响应中的JSON字符串日期转换为“ 2016/8/24”格式。我做了一个dateFilter.js,它没有按我预期的那样工作,所以这是我尝试过的。
这是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>
因此,我将如何进行转换?
最后说明:现在没有过滤器:2016年7月25日12:00:00想要
转换为7/25/2016
帮助将不胜感激。
最佳答案
只需尝试此代码。我认为这可以解决您的问题:
<td>{{emp.DOB | dateFormat}}</td>
角度过滤器:
.filter('dateFormat', function() {
return function(dt) {
var dt = new Date(dt);
return (dt.getMonth()+1)+'/'+dt.getDate()+'/'+dt.getFullYear();
};
})
关于javascript - 如何将JSON字符串日期转换为自定义格式日期?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39114559/