Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaScript Moment.js正在尝试获取两个日期时间之间的差异_Javascript_Date_Datetime_Momentjs_Ticker - Fatal编程技术网

JavaScript Moment.js正在尝试获取两个日期时间之间的差异

JavaScript Moment.js正在尝试获取两个日期时间之间的差异,javascript,date,datetime,momentjs,ticker,Javascript,Date,Datetime,Momentjs,Ticker,我试图得到两个DateTimestamp之间的区别,这两个DateTimestamp看起来如下: 2018-08-22 00:00:00 2018-08-11 15:34:31 我想以天-小时-分钟-秒为单位显示时间差 到目前为止,我这样做了,但它总是返回: import moment from 'moment'; const calc = { render () { moment().format(); this.calcDifference()

我试图得到两个DateTimestamp之间的区别,这两个DateTimestamp看起来如下:

2018-08-22 00:00:00 
2018-08-11 15:34:31
我想以天-小时-分钟-秒为单位显示时间差

到目前为止,我这样做了,但它总是返回:

import moment from 'moment';

const calc = {

    render () {
        moment().format();
        this.calcDifference();
    },

    calcDifference() {
        let releaseDate = document.getElementById('release-at').value;
        let currentDate = document.getElementById('current-date').value;

        let ms = moment(
            releaseDate,
            "DD-MM-YYYY HH:mm:ss").diff(moment(currentDate,
            "DD-MM-YYYY HH:mm:ss")
        );
        let d = moment.duration(ms);
        let s = Math.floor(d.asHours()) + moment.utc(ms).format(":mm:ss");

        console.log(ms);
        console.log(d);
        console.log(s);
    }

};

    calc.render();

calcDifference
函数中,可以执行以下操作

let releaseDate = moment('2018-09-22 00:00:00');
let currentDate = moment('2018-08-11 15:34:31');

const diff = releaseDate.diff(currentDate);
const diffDuration = moment.duration(diff);

console.log(`
  ${diffDuration.months()} months
  ${diffDuration.days()} days
  ${diffDuration.hours()} hours
  ${diffDuration.minutes()} minutes
  ${diffDuration.seconds()} seconds left!`);
如果发布日期当前日期元素中的值具有格式
DD-MM-YYYY HH:MM:ss
,使用

let releaseDate = moment(document.getElementById('release-at').value, 'DD-MM-YYYY HH:mm:ss');
let currentDate = moment(document.getElementById('current-date').value, 'DD-MM-YYYY HH:mm:ss');
这是一张工作票


我希望有帮助!:)

只有在时间小于24小时的情况下,时间才能产生差异。您需要编写自己的计算。您的时间戳与您试图解析的格式不匹配。@H77 yes true MS返回此347094234000,s返回此96415:03:54您将如何继续?@tigerle它是否工作?我无法重现这个错误:太棒了,这工作得很完美,你怎么能制作一个股票代码来说明哪一个是倒数秒,然后是分钟等等?@tigerle当然!我不想那么宠坏你,但我今天有精力:)给我一杯moment@tigerel-这里已经有倒数计时了,如果您有一个问题没有在这里讨论,请再问一个问题。@Theo请不要在其他网站上用代码回答评论中的问题。新问题应作为新问题提出。这里已经有很多关于计时器和倒计时的问题,这些问题涵盖了代码中未解决的问题。