如何获得hh:mm:ss in angularjs-datepicker

时间:2017-12-15 11:51:16

标签: angularjs datepicker angular-daterangepicker

嗨我使用angularjs datepicker功能使用npm i angularjs-datepicker工作正常,但我需要显示时间像(15-12-2017 05:14 PM)帮助如何格式化datepicker这里我提到引用链接也很累来自控制器它来了,但当我点击错误显示的日期选择日期时。有任何指定格式有帮助或如何解决该问题

https://www.npmjs.com/package/angularjs-datepicker

HTML

<div class="hackyhack" datepicker datepicker-class="test-custom-class" 
   date-format="dd-MM-y" >
 <input type="text" id="AddQuoteDate" name="QuoteDate" size="4"
 ng-model="QuoteDate" placeholder="Enter Quote Date"
 ng-change="QuoteDatechange(QuoteDate)"
 class="form-control angular-datepicker-input"
 required title="Enter Quote Date"/>
  </div>

控制器

  $scope.QuoteDatechange = function (Quotedate) {
 var date = new Date();
            var hours = date.getHours() > 12 ? date.getHours() - 12 : date.getHours();
            var am_pm = date.getHours() >= 12 ? "PM" : "AM";
            hours = hours < 10 ? "0" + hours : hours;
            var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
            var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
              time = hours + ":" + minutes + ":" + seconds + " " + am_pm;
            $scope.QuoteDate = Quotedate + " " + time;
            }

2 个答案:

答案 0 :(得分:0)

根据DOCs,您必须使用AngularJs date format

  

选项:date-format =“”

     

输入:字符串

     

默认:字符串(新日期())

     

说明:设置您要使用的日期格式,请参阅列表here

<datepicker date-format="dd-MM-yyyy hh:mm a" selector="form-control">
   <input type="text" ng-model="QuoteDate"/>
</datepicker>

答案 1 :(得分:0)

它为我工作。试试这个。希望它会对你有所帮助。

var currentdate = new Date();
var hours = currentdate.getHours();
var minutes = currentdate.getMinutes();
var ampm = hours >= 12 ? 'pm' : 'am';
hours = hours % 12;
hours = hours ? hours : 12; // the hour '0' should be '12'
minutes = minutes < 10 ? '0'+minutes : minutes;
var strTime = hours + ':' + minutes + ampm;
$scope.QuoteDate = pad(currentdate.getDate()) + "/"
                + pad(currentdate.getMonth()+1)  + "/"
                + currentdate.getFullYear() + " "
                + strTime;

function pad(s) { return (s < 10) ? '0' + s : s; }


<input type="text" id="AddQuoteDate" name="QuoteDate" size="4" ng-model="QuoteDate" placeholder="Enter Quote Date" class="form-control angular-datepicker-input" required title="Enter Quote Date"/>

   $('#AddQuoteDate').datetimepicker({
        minDate:new Date(),
        format: 'DD/MM/YYYY hh:mma'
    });