将C#DateTime转换为角日期 [英] Convert C# DateTime To Angular Date
问题描述
我有一个绑定到从MVC JsonResult填充的转发器的Angular集合.
I have an Angular collection bound to a repeater that is populated from an MVC JsonResult.
$scope.loadData = function () {
$http.get('/Product/GetDiscountCodes')
.then(function (result) {
console.log(result);
$scope.discountCodes = result.data.DiscountCodes;
$scope.selected = {};
});
};
中继器中的一个字段填充了日期.从服务器返回的日期是C#DateTime,如下所示:
One of the fields in the repeater is populated with the date. The date returned from the server is C# DateTime which looks like this:
1/5/2015 12:02:00 AM
1/5/2015 12:02:00 AM
但是当它被Angular绑定时,显示如下:
However when it is bound by Angular is displays like this:
/日期(1420434120000)/
/Date(1420434120000)/
如何获取日期以mm/dd/yyyy格式正确显示在我的文本框中?
How can I get the date to display properly in my textfield in mm/dd/yyyy format?
我尝试了ui-utils日期格式,但这没用.
I tried the ui-utils date format but that didn't work.
我还尝试创建自己的指令,该指令可以正确格式化日期,但是当save事件发生时,发送到服务器端方法的日期都被阻塞了.
I also tried creating my own directive which formats the date properly but when the save event happens, the date send to the server side method is all jacked up.
function inputDate() {
return {
require: 'ngModel',
link: function (scope, element, attrs, ngModelController) {
ngModelController.$parsers.push(function (data) {
if (data === null) { return null; }
var d = moment(data).format('YYYY-MM-DD');
return d;
});
ngModelController.$formatters.push(function (data) {
if (data === null) { return null; }
return moment(data).format('MM/DD/YYYY');
});
}
}
}
angular.module('discountApp')
.directive('inputDate', inputDate);
感谢您的帮助!
推荐答案
使用{sampledate.slice(6,-2)|日期:'dd/MM/yyyy'}
use the {sampledate.slice(6, -2) | date:'dd/MM/yyyy'}
这篇关于将C#DateTime转换为角日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!