时刻如何获得小时之间的差异

时间:2017-06-19 10:22:19

标签: javascript momentjs

我正在尝试创建几个小时的提醒我正在使用momentJs,var subtract = moment.duration (b - l) .hours ();然后在转换时我得到当前和固定时间之间的差异,现在我遇到的问题是当时钟转到第二天转换没有考虑到它来自前一天22:00到02:00。想想24小时的重播,但是有些人不会得到正确的区别是否有更有效的方法来执行计算?

我的解决方案是实现twix库,它使用以下代码行给出了不同的小时数

var subtraction = moment(b).twix(l).length("hours");

我留下了我的代码。

        timer4: function(timer){
           var t =  Meteor.users.find({ id: this.userId}).fetch();
            for (var k = 0; k < t.length; k++) {
                var tzone = t[k].profile.timezone;
                var data = AddMed.find({ userId: t[k]._id}).fetch();
                for (var i = 0; i < data.length; i++) {
                var TimerD = momentT.tz(timer,tzone).format('HH:mm');
                  var h = data[i].time;
                  var resultado = data[i].createdAt       
                   var b = moment(TimerD,'HH:mm');
       var asd = momentT.tz(data[i].createdAt,tzone).format('HH:mm');
                   var l = moment(asd,'HH:mm');
                   console.log('creadoConvertido:', l)
                   console.log('timerServerConvertido:', b)
                   var resta =  moment.duration(b - l).hours();
                   resta = Math.abs(resta);
            var hSystem = momentT.tz(timer, tzone).format('HH:mm');
                   var fecha = moment(new Date).format('YYYY-MM-DD');      
              if ( fecha == data[i].date && data[i].time == hSystem) { 
 Meteor.call('AddCheckInit', data[i]._id,t[k]._id,k, function(error) { 
                 if (error) { 
                    console.log('error', error); 
                     } 
                  });
               }  
  if (resta == data[i].recordatorio && data[i].check == 'true'){
                 console.log('entre remminder')
                    var SendP = {
                        name: data[i].NameMed,
                        detalles: data[i].detalles,
                        date: data[i].date,
                        time: data[i].time,
                        recordatorio: data[i].recordatorio,
                        idioma: t[k].profile.idioma,
                        userid: t[k]._id,
                        id: data[i]._id,
                        type: 'medicinas',
                        createdAt: data[i].createdAt,
                        k:i,
                        resta: resta
                      }
/// send push notifications
      Meteor.call("serverNotification",SendP);
      Meteor.call('updateCreated', SendP,k, function(error, success) { 
                        if (error) { 
                          console.log('error', error); 

                        } 
                        if (success) { 
                           console.log("Cambios Realizandos Existosamente")
                           console.log('succes',success.createdAt)
                        } 
                      });
                     } 
                     else {
                       console.log('contando. medicinas');
                     }
                       console.log('/****end logs*****/');
                    }

                 /*****************end************************/
                }

            }

   //**** My meteor Methodos ***********/
     updateCreated: function(SendP,k){
      var t =  Meteor.users.find({ id: this.userId}).fetch();
      var tzone = t[k].profile.timezone;
       console.log('mi zona', tzone)
       var createdAt = momentT.tz(new Date(), tzone).format();
                  console.log('creado el',createdAt);
                  AddMed.update({
                      '_id': SendP.id
                  }, {
                      '$set': {
                         'createdAt': createdAt,
                      }
                  });
                  return createdAt;
              },
     AddCheckInit: function(id,userId,k){
      console.log('AddCheckInit',id,'USER',userId);
       var t =  Meteor.users.find({ id: this.userId}).fetch();
       var tzone = t[k].profile.timezone;
       console.log('mi zona', tzone)
      var createdAt = momentT.tz(new Date(), tzone).format();
      console.log('creado el',createdAt);
      AddMed.update({
                '_id': id
              }, {
            '$set': {
          'check': 'true',
         'createdAt': createdAt,
          }
          });
          } ,

1 个答案:

答案 0 :(得分:0)

我的解决方案基于以下

  timer4: function(timer) {
    var checkSendP = false;
    var t = Meteor.users.find({
      id: this.userId
    }).fetch();
    for (var k = 0; k < t.length; k++) {
      var tzone = t[k].profile.timezone;
      var data = AddMed.find({
        userId: t[k]._id
      }).fetch();
      for (var i = 0; i < data.length; i++) {
        /*----------------------GLOBAL-------------------------*/
       var TimerD = momentT.tz(timer, tzone).format('HH:mm');
       var fecha1 = momentT.tz(timer, tzone).format('YYYY-MM-DD HH:mm:ss');
       var fecha2 = moment(data[i].createdAt, "YYYY-MM-DD HH:mm:ss");
       var resta = Math.abs(fecha2.diff(fecha1, 'H'));
       var hSystem = momentT.tz(timer, tzone).format('HH:mm');
       var fecha = moment(new Date).format('YYYY-MM-DD');
       /*-----------------------------------*/
        console.log('/*Server logs CronJobs*/');
        console.log('profileName:', t[k].profile.name);
        console.log(`medicina ${data[i].NameMed}`);
        console.log('tzone:', tzone);
        console.log('DateSistema:', new Date())
        console.log('UTC:', momentT.tz.guess());
        console.log("fecha1", fecha1);
        console.log("fecha2", fecha2);
        console.log('cTimer:', TimerD);
        console.log('created el:', data[i].createdAt)
        console.log('timerCronJobs:', timer);
        console.log('resta Abs:', resta, 'Horas');
        console.log('recordatorio', data[i].reminder);
        console.log('resta', resta);
        console.log('date', data[i].date);
        console.log('fecha', fecha);
        console.log('hSystem', hSystem);
        console.log('hora', data[i].time);
        /*-----------------------------------*/
        /*-------------------GLOBAL-----------------------*/
        var SendP = {
          name: data[i].NameMed,
          detalles: data[i].detalles,
          date: data[i].date,
          time: data[i].time,
          recordatorio: data[i].recordatorio,
          idioma: t[k].profile.idioma,
          userid: t[k]._id,
          id: data[i]._id,
          type: 'medicinas',
          createdAt: data[i].createdAt,
          k: i,
          resta: resta
        }
        if (fecha == data[i].date && data[i].time == hSystem) {
          console.log('entre');
          Meteor.call('AddCheckInit', data[i]._id, t[k]._id, k, function(error) {
            if (error) {
              console.log('error', error);
            }
          });
        }
          console.log('check',data[i].check);
          console.log(resta == data[i].reminder && data[i].check == 'true');
        if (resta == data[i].reminder && data[i].check == 'true') {
          console.log('entre remminder');
          data[i].checkSendP = 'true';
        }
        console.log(data[i].checkSendP)
        if (data[i].checkSendP == 'true') {
            data[i].checkSendP = 'false'
           Meteor.call("serverNotification", SendP);
           Meteor.call('updateCreated', SendP, k, data[i].checkSendP, new Date(), function(error, success) {
             if (error) {
               console.log('error', error);
             }
             if (success) {
               console.log("Cambios Realizandos Existosamente")
               console.log('succes', success.createdAt)
             }
           });
        }
        console.log('/****end logs*****/');
      }

      /*****************end************************/
    }