【发布时间】:2016-10-14 19:53:47
【问题描述】:
我使用来自https://github.com/icambron/moment-countdown 的这个简单脚本来进行简单的倒计时。我正在使用下面的代码。
使用的代码:
$interval(function(){
$scope.nextDate = moment().countdown($scope.nextDateGet,
countdown.DAYS|countdown.HOURS|countdown.MINUTES|countdown.SECONDS
);
$scope.daysCountdown = moment($scope.nextDate).format('dd');
$scope.hoursCountdown = moment($scope.nextDate).format('hh');
$scope.minutesCountdown = moment($scope.nextDate).format('mm');
$scope.secondsCountdown = moment($scope.nextDate).format('ss');
},1000,0);
这给出了正确的输出
$scope.nextDate.toString();
但这包含一个字符串,其中包含剩余的天、小时、分钟和秒。所以我决定我想用这个把这个字符串分成4个字符串:
$scope.daysCountdown = moment($scope.nextDate).format('dd');
$scope.hoursCountdown = moment($scope.nextDate).format('hh');
$scope.minutesCountdown = moment($scope.nextDate).format('mm');
$scope.secondsCountdown = moment($scope.nextDate).format('ss');
输入示例
2016-10-15 10:00:00 // $scope.nextDateGet
所需的输出是这样的:
0 // (days)
12 // (hours)
24 // (minutes)
30 // (seconds)
但我似乎无法格式化剩余的日子,我得到这个输出:
Fr // Shortcode for the day the item is scheduled => I need the remaining days in this case that would be 0. The other formatting is correct.
【问题讨论】:
-
$scope.daysCountdown = moment($scope.nextDate).format('D');似乎是有效的。仅当剩余天数为0时才不起作用。它将剩余天数设置为14。 -
如果
$scope.nextDate.toString()有正确的输出,而你只想把它分成4个,你能不能不直接用$scope.nextDate.toString().split(',')得到一个由四个字符串组成的数组?从演示站点上的输出来看,我认为您不想在countdown()的输出上调用moment()。 -
@Mike McCaughan True 但也需要从字符串中删除自动格式化文本,例如 ... Days ... Hours ... Minuts ... Seconds。
标签: angularjs momentjs countdown