绑定到Date() - 对象时如何格式化input [time]的值 [英] How to format the value of input[time] when bound to Date()-object

查看:123
本文介绍了绑定到Date() - 对象时如何格式化input [time]的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将变量绑定到time类型的输入字段,但显示的格式错误。

I am binding a variable to an input-field of type time, but the displayed format is wrong.

它显示如下的时间: 08:54:30,088
我真正需要的是这样的格式: 08:54

It displays the time like this: 08:54:30,088 What I actually need is a format like this: 08:54.

我试图用过滤器设置输入字段的值( value = {{datetime.date | date:'HH:mm'}} )但我的编辑说我这样做的方式是错误的。有什么想法吗?

I tried to set the value of the input field with a filter ( value={{ datetime.date | date : 'HH:mm' }} ) but my editor says the way I do this is wrong. Any ideas?

这里的compltete代码:

Here the compltete code:

 <input id="rep_time" class="form-control" type="time" ng-model="datetime.time" value={{ datetime.date | date : 'HH:mm' }}>



JS



JS

 app.controller( 'newCtrl', function( $scope ) {  

     $scope.datetime = {
         date: new Date(),
         time: new Date()
       };
 } );


推荐答案

我通过定义Date()来规避问题 - 对象有点。它有效,但我不喜欢这种双重定义。

I circumvented the problem by defining the Date()-object a bit. It works but I don't like this way of double defining.

 <input class="form-control" type="date" ng-model="datetime.date" placeholder="yyyy-MM-dd" min="2016-01-01" required />

 <input class="form-control" type="time" ng-model="datetime.time">



JS



JS

$scope.datetime = {
        date: new Date(),
        time: ''
    };

$scope.datetime.time = new Date(
            $scope.datetime.date.getFullYear(),
            $scope.datetime.date.getMonth() + 1,
            $scope.datetime.date.getDate(),
            $scope.datetime.date.getHours(),
            $scope.datetime.date.getMinutes() );



更新js



带有这个想法使用来自 Jimbrooism 的$ filter我找到了一个更短的方法!

UPDATE of js

with the idea of using a $filter from Jimbrooism I found a shorter way!

$scope.datetime = {
     date: new Date(),
     time: new Date( $filter( 'date' )( new Date(), 'yyyy-MM-dd HH:mm' ) )
   };

这篇关于绑定到Date() - 对象时如何格式化input [time]的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆