【问题标题】:Incorrect day format returned from momentJS countdown从 momentJS 倒计时返回的日期格式不正确
【发布时间】: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


【解决方案1】:

如果剩余天数不为 0,则以下输出是正确的:

$scope.daysCountdown = moment($scope.nextDate).format('D'); 

如果剩余天数为 0,它会将剩余天数设置为 14,因此这个解决方法可以解决问题:

if(moment($scope.nextDate).isSame(moment(), 'day')){
    $scope.daysCountdown = 0;
} else {
    $scope.daysCountdown = moment($scope.nextDate).format('D');
}

欢迎任何改进此代码的建议。

【讨论】:

    猜你喜欢
    • 2020-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-24
    • 2014-04-10
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多