简体   繁体   English

Moment.js,自定义日期格式和倒计时与倒计时的区别

[英]Moment.js, custom date format and countdown from difference with callback

I am using Moment.js for this. 我为此使用Moment.js。 I tried looking docs but could find a solution that helps me at this scenario. 我尝试查找文档,但可以找到一种在这种情况下对我有帮助的解决方案。

function countDown(scheduler){
  var time = moment().local("fi").format("L hh:mm:ss");

  console.log("Current time: "+time);
  console.log("Scheduler: "+scheduler);
  console.log("Difference: "+(time-scheduler));

}

Current time: 27.07.2017 12:23:21
Scheduler: 27.07.2017 12:16:31
Difference: NaN

I am trying to get the difference between time and scheduler , then return value as minutes and it should countdown to 0 and then fire callback function 我试图获取时间调度程序之间的差异,然后以分钟为单位返回值,并且应该倒数到0,然后触发回调函数

I have no idea how to parse that time to an object. 我不知道如何解析该时间到一个对象。 I know that I have to do that before I can get the difference. 我知道我必须先这样做,然后才能有所作为。

EDIT 3: 编辑3:

  function countDown(scheduler){

    var time = moment();
    var schedulerMoment = moment(scheduler, "L hh:mm:ss");

    console.log("Current time: " + time.local("fi").format("L hh:mm:ss"));
    console.log("Scheduler: "+scheduler);
    console.log("Difference: "+(time.diff(schedulerMoment, "minutes")));
  }
  • Current time: 07/27/2017 01:23:00 当前时间:07/27/2017 01:23:00
  • Scheduler: 27.07.2017 12:55:14 日程安排:27.07.2017 12:55:14
  • Difference: NaN 差异:NaN

Creating moment object with custom format is supported 支持使用自定义格式创建矩对象

moment(time, format)

You can also get different in minutes like this 您也可以在几分钟之内变得与众不同

time.diff(scheduler, 'minutes')

So your code can look like this 所以你的代码看起来像这样

function countDown(scheduler){
    var time = moment();
    var schedulerMoment = moment(scheduler, "DD.MM.YYYY hh:mm:ss");

    console.log("Current time: " + time.local("fi").format("L hh:mm:ss"));
    console.log("Scheduler: "+scheduler);
    console.log("Difference: "+(time.diff(schedulerMoment, "minutes")));
}

You can try this code :- 您可以尝试以下代码:-

function countDown(schedule){ 
  var time = moment().local("fi").format("L hh:mm:ss");
  var schedule = new Date(schedule)
  var momentschedule = moment(schedule);
  console.log("Current time: "+time);
  console.log("Scheduler: "+schedule);
  var duration = moment.duration(momentschedule.diff(time));
 var minutes = duration.asMinutes();

  console.log("Difference: "+(minutes));
}
countDown("27.07.2017 12:16:31");//pass schedule date string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM