Javascript 角猫鼬:能';不要操纵日期
我已经寻找了很长一段时间的解决办法,但没有任何帮助 我有一个运行Mongoose和Express的Angular JS应用程序 我想将日期作为简单表单中的对象存储。 但当我提交表单时,日期存储为字符串而不是对象。 所以我不能这样做:Javascript 角猫鼬:能';不要操纵日期,javascript,angularjs,mongodb,date,object,Javascript,Angularjs,Mongodb,Date,Object,我已经寻找了很长一段时间的解决办法,但没有任何帮助 我有一个运行Mongoose和Express的Angular JS应用程序 我想将日期作为简单表单中的对象存储。 但当我提交表单时,日期存储为字符串而不是对象。 所以我不能这样做: tache.start.getDate(); 这是我的表格: <form name="formTache" ng-submit="addTache(tache)"> <div class="form-group row">
tache.start.getDate();
这是我的表格:
<form name="formTache" ng-submit="addTache(tache)">
<div class="form-group row">
<div class="col-lg-12">
<input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date"/>
</div>
</div>
<div class="form-group row">
<div class="text-right col-lg-12">
<button type="submit" class="btn btn-default">Ajouter</button>
</div>
</div>
这是我的控制器:
angular.module('personaldashboard.tache', [])
.controller('TachesCtrl', function($scope, Taches, Progress, toaster) {
$scope.tache = new Taches();
var refreshTache = function() {
$scope.taches = Taches.query();
$scope.tache = ''
}
refreshTache();
$scope.addTache = function(tache) {
Taches.save(tache,function(tache){
refreshTache();
});
};
$scope.updateTache = function(tache) {
tache.$update(function(){
refreshTache();
});
};
$scope.removeTache = function(tache) {
tache.$delete(function(){
refreshTache();
});
};
$scope.editTache = function(id) {
$scope.tache = Taches.get({ id: id });
};
$scope.deselectTache = function() {
$scope.tache = ''
}
$scope.editTache_Projet = function(tache, projetId) {
tache.projet = projetId;
tache.$update(function(){
refreshTache();
});
};
});
$scope.addTache = function(tache) {
tache.start = new Date(tache.start);
tache.end = new Date(tache.end);
Taches.save(tache,function(tache){
refreshTache();
});
};
以下是我得到的:
{ "_id": "58a99df24975ad0104c692b1", "title": "Test", "start": "2017-02-24T23:00:00.000Z" }
那么,为什么我会得到一个类似于“2017-02-24T23:00:00.000Z”
的字符串作为我的日期而不是对象,而我的Mongoose模式指定开始:{type:date,default:new date()}
谢谢你的帮助
编辑
感谢Saurabh Agrawal,我在控制器中提交时尝试转换日期:
angular.module('personaldashboard.tache', [])
.controller('TachesCtrl', function($scope, Taches, Progress, toaster) {
$scope.tache = new Taches();
var refreshTache = function() {
$scope.taches = Taches.query();
$scope.tache = ''
}
refreshTache();
$scope.addTache = function(tache) {
Taches.save(tache,function(tache){
refreshTache();
});
};
$scope.updateTache = function(tache) {
tache.$update(function(){
refreshTache();
});
};
$scope.removeTache = function(tache) {
tache.$delete(function(){
refreshTache();
});
};
$scope.editTache = function(id) {
$scope.tache = Taches.get({ id: id });
};
$scope.deselectTache = function() {
$scope.tache = ''
}
$scope.editTache_Projet = function(tache, projetId) {
tache.projet = projetId;
tache.$update(function(){
refreshTache();
});
};
});
$scope.addTache = function(tache) {
tache.start = new Date(tache.start);
tache.end = new Date(tache.end);
Taches.save(tache,function(tache){
refreshTache();
});
};
可悲的是,这并没有改变什么:(
我仍然有一个日期作为字符串
"2017-02-20T23:00:00.000Z"
编辑
我还试图添加一个指令
.directive("formatDate", function(){
return {
scope: {ngModel:'='},
link: function(scope) {
if (scope.ngModel) {
scope.ngModel = new Date(scope.ngModel);
}
}
}
})
在我的表格里打电话
<form name="formTache" ng-submit="addTache(tache)">
<div class="form-group row">
<div class="col-lg-12">
<input name="name" class="form-control" placeholder="Nom" type="text" ng-model="tache.title"/>
</div>
</div>
<div class="form-group row">
<div class="col-lg-12">
<input id="date" name="date" class="form-control" placeholder="Date" ng-model="tache.start" type="date" formatDate/>
</div>
</div>
<div class="form-group row">
<div class="text-right col-lg-12">
<button type="submit" class="btn btn-default">Ajouter</button>
</div>
</div>
阿约特
但一切都没有改变
还有其他想法吗?通过搜索,我发现我忽略了真正的问题 我的日期是日期对象 当我这么做的时候
$scope.test = new Date([2017,2,15]);
<pre>{{test}}</pre>
<pre>{{test.getDate()}}</pre>
我什么也得不到:(
此代码:
{{tache.start}
显示如下日期“2017-02-20T23:00:00.000Z”
但是
{{tach.start.getDate()}
不显示任何内容
我错过了什么?现在使用
新日期()更改它。
谢谢你的回答。我尝试了在我的传统帖子中显示的内容。但它不起作用。我错过了什么吗?
{{tache.start}}
<pre>{{tache.start.getDate()}}</pre>