- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 MomentJS (2.22.2
) 和 Rxjs (6.2.2
) interval
创建倒计时。但是当我到达某些时间点时,我的倒计时变得疯狂并输出负数,例如,它应该减去一天并从 23 小时开始。
关于我应该如何解决这个问题有什么帮助吗?
示例
本地时间:2018-09-05T18:49
结束时间:2018-11-03T18:00
这导致:
我的代码
'this.days
', 'this.hours
', 'this.minutes
' & 'this.seconds
' 是局部变量,保存结果时间的字符串表示(字符串格式,因为我想显示前导零)
interval(1000).subscribe(() => {
const now = moment();
let timeLeft = this.endDate.diff(now, 'milliseconds', true);
let endDate = moment(this.endDate); // Use 'moment' to make a new copy
// Days
const days = Math.floor(moment.duration(timeLeft).asDays());
console.warn(moment.duration(timeLeft).asDays());
this.days = Countdown.addLeadingZeros(days.toString(), 3);
endDate = endDate.subtract(days, 'days');
timeLeft = endDate.diff(now, 'milliseconds', true);
// Hours
const hours = Math.floor(moment.duration(timeLeft).asHours());
console.warn(Math.round(moment.duration(timeLeft).asHours()));
this.hours = Countdown.addLeadingZeros(hours.toString(), 2);
endDate = endDate.subtract(hours, 'hours');
timeLeft = endDate.diff(now, 'milliseconds', true);
// Minutes
const minutes = Math.floor(moment.duration(timeLeft).asMinutes());
this.minutes = Countdown.addLeadingZeros(minutes.toString(), 2);
endDate = endDate.subtract(minutes, 'minutes');
timeLeft = endDate.diff(now, 'milliseconds', true);
// Seconds
const seconds = Math.floor(moment.duration(timeLeft).asSeconds());
this.seconds = Countdown.addLeadingZeros(seconds.toString(), 2);
});
有没有人看到我在这里做错了什么或者我可以如何改进这段代码?
最佳答案
由于夏令时的变化,您的代码中出现了负数。
在英国,2018-09-05
和 2018-11-03
之间的夏令时发生了变化。事实上它是在 2018-10-28
上。如果我在这两侧选择两个日期,我可以重现您的问题,但如果两个日期都在同一侧,则不能。我猜你住的地方在 2018-09-05
和 2018-11-03
之间也有夏令时的变化。
避免此类问题的最简单方法是使用 UTC 时间计算时差。而不是写作
const now = moment();
写
const now = moment().utc();
并确保 this.endDate
是在 UTC 而不是本地时间中创建的。
关于javascript - Momentjs 倒计时 - 持续时间显示负数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52190158/
赏金:对于提供代码以使此子程序与负数一起工作的任何人,+50 信誉点。 我编写了一个 MIPS 程序来将华氏温度转换为摄氏温度。它打开自己的输出窗口(即 UART)并正确显示摄氏值。它在从 C 调用到
我得到了以下代码 # On va convertir en somme monétaire # We convert our integer into a way to read money
我得到了以下代码 # On va convertir en somme monétaire # We convert our integer into a way to read money
我使用以下 RegEx 基本上过滤掉任何文本,并接受数字 + 运算符。 ([-+]?[0-9]*\.?[0-9]+[\/\+\-\*])+([-+]?[0-9]*\.?[0-9]+) 所以它抓取 1+
我有一个查询,它计算我在查询中使用 union all 的平均值,以便获取我最终使用 max 函数的数据。 当联合返回结果时,如下所示:- col 1 col2 1 0
我有这样一个类: public class SpiralGenerator implements Iterator> { private void generate(int pos, E...
A = numpy.matrix([[36, 34, 26], [18, 44, 1], [11, 31, 41]]) X1 = numpy.matrix([[462
我有一个应用程序,其中有一个显示硬币 00 的 TextView ,一个按钮显示奖励视频广告,为用户提供 10 个硬币,还有一个购买按钮,将硬币减少 30 个。现在,当用户有 30 个硬币时,单击购买
话不多少,直接附上代码实例,仅供参考 ? 1
我有一系列正数和负数,我想将每个数字的绝对值增加一个,同时仍保持正数/负数。0.2 -> 1.2-0.3 -> -1.3我怎样才能做到这一点? 最佳答案 让我们尝试使用numpysign s=pd.S
我有这段代码,只允许在 keypress() 的输入字段中输入数字 if (e.which != 8 && e.which != 0 && (e.which 57)) { return fa
我试图用“-1”作为所有值填充二维数组。我正在使用的代码是: int c [] []=new int[4][4]; Arrays.fill(c,-1) 这会引发以下错误: Exception in t
在学校作业中,我们应该编写一个程序,该程序接受一个数字并将其分为三个部分:1. 检查数字是正数还是负数2. 整数(大小)3.小数部分 要求是应该有一个自己的函数,名为separate,具有输入和输出参
有没有什么方法可以在 C# 中执行整数除法(没有 float 或小数,我需要保持这个非常快)来向下舍入数字? 默认除法只是丢弃分数参数。考虑: 1 / 2 = 0 // That is correc
我正在使用 matplotlib 为报告生成图表,并指定我自己的样式表来指定文本格式以符合报告的指定文档样式。在我的 .mplstyle 样式表中,我按如下方式指定字体系列: font.family
在 C++11 中,如果我们尝试使用全局运算符 new 分配负大小的数组,它会抛出 std::bad_array_new_length,但是 C++98/C++03 呢?是 UB 还是会抛出 std:
我试过 scanf("%u",&number) 并且我输入了负数问题是当我 printf("%d",number) 我得到负数。我认为这会阻止我读取负数。scanf("%d",&number) 和 s
我的任务是解释一些看似奇怪的C代码行为(在x86上运行)。我可以轻松完成所有其他工作,但是这确实让我感到困惑。 代码段1输出-2147483648 int a = 0x80000000; int
js有问题吗? if("hello".indexOf("world")) { // I forgot to add > -1 here console.log("hello world");
我正在尝试使用 Antlr 4 设置一个简单的计算器。 语法: grammar calcGrammar; input : expression EOF; expression : MINUS
我是一名优秀的程序员,十分优秀!