gpt4 book ai didi

javascript - 将日期绑定(bind)到输入字段

转载 作者:行者123 更新时间:2023-11-30 14:54:50 24 4
gpt4 key购买 nike

我有这样的日期格式 /Date(1495623815523)/ .如何在正常的输入字段中绑定(bind)它 dd-MM-yy格式。

var app=angular
.module('plunker', [])
.controller('MainCtrl', function($scope,$filter) {
$scope.dateString = '/Date(1495623815523)/';
$scope.formattedDate = $filter('jsonDate')($scope.dateString);
})
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="plunker" ng-controller="MainCtrl">

<form name="myForm">
<label for="exampleInput">Date input</label>

<input
type="date"
ng-model="dateString"
/><br>
//its working for label<br>

<label>{{dateString | jsonDate | date:'dd-MM-yyyy'}}</label>
</form>

</section>

最佳答案

您必须将您的 ng-model 值转换为格式正确的日期对象,因为您使用的是“日期”输入类型。

var app=angular
.module('plunker', [])
.controller('MainCtrl', function($scope,$filter) {
$scope.dateString = '/Date(1495623815523)/';
$scope.formattedDate = $filter('jsonDate')($scope.dateString);
$scope.dateStringFormatted =$filter('date')($scope.formattedDate, "yyyy-MM-dd");
console.log( $scope.dateStringFormatted);
})
app.filter('jsonDate', function () {
return function (date) {
return new Date(date.match(/\d+/)[0] * 1);
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<section ng-app="plunker" ng-controller="MainCtrl">

<form name="myForm">
<label for="exampleInput">Date input</label>

<input
type="date"
ng-model="dateStringFormatted"
/><br>
//its working for label<br>

<label>{{dateStringFormatted | date:'dd-MM-yyyy'}}</label>
</form>

</section>

关于javascript - 将日期绑定(bind)到输入字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47468380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com