我最近用moment.js实现了一个日期选择器。由于某种原因,它显示错误的日期。我在日期选择器之外显示了日期,很好。我控制台。记录变量以查看存储的内容,结果证明是我想要的格式。但是,当我调用日期选择器中的数据时,它显示的格式错误。它应该与日期选择器下面输出的内容完全相同。有人可以解释我出错的地方吗?
日期选择器和下方输出的正确日期:
<div class="datepicker-container">
<div class="date-from">
From:
<datepicker date-set="{{yesterday}}" selector="form-control" date-max-limit="{{today}}" class="date-picker">
<div class="input-group">
<input class="form-control" placeholder="Choose a date"/>
<span class="input-group-addon" style="cursor: pointer">
<i class="glyphicon glyphicon-calendar"></i>
</span>
</div>
</datepicker>
</div>
<div class="date-too">
To:
<datepicker date-set="{{today}}" selector="form-control" date-min-limit="{{yesterday}}" class="date-picker">
<div class="input-group">
<input class="form-control" placeholder="Choose a date"/>
<span class="input-group-addon" style="cursor: pointer">
<i class="glyphicon glyphicon-calendar"></i>
</span>
</div>
</datepicker>
</div>
</div>
<h4>{{yesterday}} and {{today}}</h4>
定义日期:
var currentDate = moment(new Date()).format("DD/MM/YYYY");
console.log("1", currentDate);
$scope.today = currentDate.toString();
console.log("2", $scope.today);
var yesterdaysDate = moment(new Date()).subtract(1, 'days').format("DD/MM/YYYY");
console.log("3", yesterdaysDate);
$scope.yesterday = yesterdaysDate.toString();
console.log("4", $scope.yesterday);
答案 0 :(得分:0)
解决了这个问题,我用日期选择器格式化日期而不是片刻。
日期选择器:
<div class="datepicker-container">
<div class="date-from">
From:
<datepicker date-set="{{yesterday}}" date-format="dd/MM/yyyy" selector="form-control" date-max-limit="{{today}}" class="date-picker">
<div class="input-group">
<input class="form-control" placeholder="Choose a date"/>
<span class="input-group-addon" style="cursor: pointer">
<i class="glyphicon glyphicon-calendar"></i>
</span>
</div>
</datepicker>
</div>
<div class="date-too">
To:
<datepicker date-set="{{today}}" date-format="dd/MM/yyyy" selector="form-control" date-min-limit="{{yesterday}}" class="date-picker">
<div class="input-group">
<input class="form-control" placeholder="Choose a date"/>
<span class="input-group-addon" style="cursor: pointer">
<i class="glyphicon glyphicon-calendar"></i>
</span>
</div>
</datepicker>
</div>
</div>
<h4>{{yesterday}} and {{today}}</h4>
<强>矩:强>
var currentDate = moment(new Date());
console.log("1", currentDate);
$scope.today = currentDate.toString();
console.log("2", $scope.today);
var yesterdaysDate = moment(new Date()).subtract(1, 'days');
console.log("3", yesterdaysDate);
$scope.yesterday = yesterdaysDate.toString();
console.log("4", $scope.yesterday);