2017-02-20 137 views
0

我有日期作为日期对象。角度js:操作对象的值

当我这样做

$scope.test = new Date([2017,2,15]); 

<pre>{{test}}</pre> 
<pre>{{test.getDate()}}</pre> 

我得到

“2017-02-14T23:00:00.000Z” 和15

所以到这里,我们是很好的。

但在我而言,当我尝试做相同的日期whitch是在另一个物体像在这个模式:

var tachesSchema = new mongoose.Schema({ 
    title : String, 
    start: {type: Date, default: new Date()}, 
    end: {type: Date, default: new Date()}, 
    comment : String, 
    state : Boolean, 
    projet : { type: ObjectId, ref: 'Projets' } 
}); 

我得到什么:(

此代码:

{{tache.start}}

显示的日期是这样 “2017-02-20T23:00:00.000Z”

<pre>{{tache.start.getDate()}}</pre> 

什么都不显示。

我错过了什么?

编辑

我省略了精确的,我想这样做在NG-重复

下面的代码给我喜欢约会“2017-02-20T23:00:00.000Z”

<pre ng-repeat="tache in taches"> 
    {{tache.start}} 
</pre> 

下面的代码给我什么

<pre ng-repeat="tache in taches"> 
    {{tache.start.getDay()}} 
</pre> 

回答

0

尝试 {{tachesSchema.st art.getDate()}}

+0

感谢您的帮助。我已经尝试过,但它没有改变任何东西:( –

0

您可以定义与功能的默认设置:

new Schema({ 
    date: { type: Date, default: Date.now } 
}) 

见文档here

+0

感谢您的帮助。我已经尝试过,但它并没有改变anithing :( –

1

当您尝试{{tache.start}}时,您会获得“String”值。您应该将其转换为Date对象(新日期(“2017-02-20T23:00:00.000Z”))并尝试getDate()方法。

或尝试以下:

new Schema({ 
    date: { type: Date, default: '15/02/2017' } 
});' 
+0

当我这样做$ scope.test = new Date([2017, 2,15]);

{{test}}
{{test.getDate()}}
我得到 “2017-02-14T23:00:00.000Z” 和15 所以我相信 “2017-02-14T23:00:00.000Z” 能被操纵为对象,我错了吗? 当我在我的真实对象“tache”中重复同样的事情时,我什么都没有得到。这很混乱:( –

+0

请你可以通过jsfiddle/codepen/jsbin分享你的示例代码吗? –