How to get difference in given time and current time in typescript is










0















I am using angular 7 and want to display elements in component html if following condition is satisfied: given time is ahead of current time.



I tried below logic



 getTimeDiff(time, date)


const dateNow = new Date()
const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

//if diff is not negative
if(diff)
return false

else
return true




HTML



<span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>


Update



Elements are displayed using *ngFor so I cannot call getTimeDiff in ngOnInit().



<div *ngFor="let result of results">
<span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>
</div>


but for some reason I get:




ViewAppointmentsComponent.html:30 ERROR Error:
ExpressionChangedAfterItHasBeenCheckedError: Expression has changed
after it was checked. Previous value: 'null: 6.0732225'. Current
value: 'null: 6.0732252777777775'.











share|improve this question




























    0















    I am using angular 7 and want to display elements in component html if following condition is satisfied: given time is ahead of current time.



    I tried below logic



     getTimeDiff(time, date)


    const dateNow = new Date()
    const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

    const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

    //if diff is not negative
    if(diff)
    return false

    else
    return true




    HTML



    <span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>


    Update



    Elements are displayed using *ngFor so I cannot call getTimeDiff in ngOnInit().



    <div *ngFor="let result of results">
    <span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>
    </div>


    but for some reason I get:




    ViewAppointmentsComponent.html:30 ERROR Error:
    ExpressionChangedAfterItHasBeenCheckedError: Expression has changed
    after it was checked. Previous value: 'null: 6.0732225'. Current
    value: 'null: 6.0732252777777775'.











    share|improve this question


























      0












      0








      0








      I am using angular 7 and want to display elements in component html if following condition is satisfied: given time is ahead of current time.



      I tried below logic



       getTimeDiff(time, date)


      const dateNow = new Date()
      const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

      const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

      //if diff is not negative
      if(diff)
      return false

      else
      return true




      HTML



      <span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>


      Update



      Elements are displayed using *ngFor so I cannot call getTimeDiff in ngOnInit().



      <div *ngFor="let result of results">
      <span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>
      </div>


      but for some reason I get:




      ViewAppointmentsComponent.html:30 ERROR Error:
      ExpressionChangedAfterItHasBeenCheckedError: Expression has changed
      after it was checked. Previous value: 'null: 6.0732225'. Current
      value: 'null: 6.0732252777777775'.











      share|improve this question
















      I am using angular 7 and want to display elements in component html if following condition is satisfied: given time is ahead of current time.



      I tried below logic



       getTimeDiff(time, date)


      const dateNow = new Date()
      const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

      const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

      //if diff is not negative
      if(diff)
      return false

      else
      return true




      HTML



      <span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>


      Update



      Elements are displayed using *ngFor so I cannot call getTimeDiff in ngOnInit().



      <div *ngFor="let result of results">
      <span *ngIf="getTimeDiff(result.endtime, result.date)"> open </span>
      </div>


      but for some reason I get:




      ViewAppointmentsComponent.html:30 ERROR Error:
      ExpressionChangedAfterItHasBeenCheckedError: Expression has changed
      after it was checked. Previous value: 'null: 6.0732225'. Current
      value: 'null: 6.0732252777777775'.








      angular typescript date angular7






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 12:00







      Harsimer

















      asked Nov 14 '18 at 11:50









      HarsimerHarsimer

      97131645




      97131645






















          2 Answers
          2






          active

          oldest

          votes


















          1














          That's a lifecycle error, saying Angular has already checked a value, but you're updating it for some reason.



          If you put a console log in your function, you will see that it's called A LOT of time.



          That's because functions, bound to directives, are called at every user interaction.



          That means that everytime it's called, it gets a new date value (+1 ms)



          To avoid that, create your date at component creation and compare on it. If you wish, you can update it some time, but not in the function itself.



          constructor(private now = new Date()) 

          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          return false

          else
          return true




          EDIT



          To avoid your function being called, you could use a variable that is update on change :



          this.timeDiff: boolean;

          ngDoCheck()
          this.timeDiff(this.result.endtime, this.result.date);


          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.timeDiff = false;

          else
          this.timeDiff = true;




          In your HTML



          <span *ngIf="timeDiff"> open </span>


          ngDoCheck is a lifecycle hook (like ngOnInit) that can be summed up by




          Function that detect changes that are not tracked by Angular







          share|improve this answer

























          • Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

            – Harsimer
            Nov 14 '18 at 12:10






          • 1





            The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

            – trichetriche
            Nov 14 '18 at 12:11












          • trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

            – Harsimer
            Nov 14 '18 at 14:16











          • @Simer Sure, I have edited my answer, feel free to try it out

            – trichetriche
            Nov 14 '18 at 14:33


















          1














          Angular runs change detection and when its find that some values which has been passed to the child component has been changed Angular throws the error ExpressionChangedAfterItHasBeenCheckedError.



          Better to create one variable to hold the information rather than calling the same function on each change detection



          different = false; //<-- hold the difference state.

          getTimeDiff(time, date)


          const dateNow = new Date()
          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.different = false //<-- change to false.

          else
          this.different = true //<-- change to true.




          html



          <span *ngIf="different"> open </span>



          Note : Do not forget to call getTimeDiff function in appropriate place like ngOnInit if you want to get one time .







          share|improve this answer

























          • I have to call getTimeDiff in *ngFor. Let me update my question.

            – Harsimer
            Nov 14 '18 at 11:56






          • 1





            With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

            – trichetriche
            Nov 14 '18 at 12:44










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299597%2fhow-to-get-difference-in-given-time-and-current-time-in-typescript-is%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          That's a lifecycle error, saying Angular has already checked a value, but you're updating it for some reason.



          If you put a console log in your function, you will see that it's called A LOT of time.



          That's because functions, bound to directives, are called at every user interaction.



          That means that everytime it's called, it gets a new date value (+1 ms)



          To avoid that, create your date at component creation and compare on it. If you wish, you can update it some time, but not in the function itself.



          constructor(private now = new Date()) 

          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          return false

          else
          return true




          EDIT



          To avoid your function being called, you could use a variable that is update on change :



          this.timeDiff: boolean;

          ngDoCheck()
          this.timeDiff(this.result.endtime, this.result.date);


          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.timeDiff = false;

          else
          this.timeDiff = true;




          In your HTML



          <span *ngIf="timeDiff"> open </span>


          ngDoCheck is a lifecycle hook (like ngOnInit) that can be summed up by




          Function that detect changes that are not tracked by Angular







          share|improve this answer

























          • Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

            – Harsimer
            Nov 14 '18 at 12:10






          • 1





            The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

            – trichetriche
            Nov 14 '18 at 12:11












          • trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

            – Harsimer
            Nov 14 '18 at 14:16











          • @Simer Sure, I have edited my answer, feel free to try it out

            – trichetriche
            Nov 14 '18 at 14:33















          1














          That's a lifecycle error, saying Angular has already checked a value, but you're updating it for some reason.



          If you put a console log in your function, you will see that it's called A LOT of time.



          That's because functions, bound to directives, are called at every user interaction.



          That means that everytime it's called, it gets a new date value (+1 ms)



          To avoid that, create your date at component creation and compare on it. If you wish, you can update it some time, but not in the function itself.



          constructor(private now = new Date()) 

          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          return false

          else
          return true




          EDIT



          To avoid your function being called, you could use a variable that is update on change :



          this.timeDiff: boolean;

          ngDoCheck()
          this.timeDiff(this.result.endtime, this.result.date);


          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.timeDiff = false;

          else
          this.timeDiff = true;




          In your HTML



          <span *ngIf="timeDiff"> open </span>


          ngDoCheck is a lifecycle hook (like ngOnInit) that can be summed up by




          Function that detect changes that are not tracked by Angular







          share|improve this answer

























          • Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

            – Harsimer
            Nov 14 '18 at 12:10






          • 1





            The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

            – trichetriche
            Nov 14 '18 at 12:11












          • trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

            – Harsimer
            Nov 14 '18 at 14:16











          • @Simer Sure, I have edited my answer, feel free to try it out

            – trichetriche
            Nov 14 '18 at 14:33













          1












          1








          1







          That's a lifecycle error, saying Angular has already checked a value, but you're updating it for some reason.



          If you put a console log in your function, you will see that it's called A LOT of time.



          That's because functions, bound to directives, are called at every user interaction.



          That means that everytime it's called, it gets a new date value (+1 ms)



          To avoid that, create your date at component creation and compare on it. If you wish, you can update it some time, but not in the function itself.



          constructor(private now = new Date()) 

          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          return false

          else
          return true




          EDIT



          To avoid your function being called, you could use a variable that is update on change :



          this.timeDiff: boolean;

          ngDoCheck()
          this.timeDiff(this.result.endtime, this.result.date);


          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.timeDiff = false;

          else
          this.timeDiff = true;




          In your HTML



          <span *ngIf="timeDiff"> open </span>


          ngDoCheck is a lifecycle hook (like ngOnInit) that can be summed up by




          Function that detect changes that are not tracked by Angular







          share|improve this answer















          That's a lifecycle error, saying Angular has already checked a value, but you're updating it for some reason.



          If you put a console log in your function, you will see that it's called A LOT of time.



          That's because functions, bound to directives, are called at every user interaction.



          That means that everytime it's called, it gets a new date value (+1 ms)



          To avoid that, create your date at component creation and compare on it. If you wish, you can update it some time, but not in the function itself.



          constructor(private now = new Date()) 

          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          return false

          else
          return true




          EDIT



          To avoid your function being called, you could use a variable that is update on change :



          this.timeDiff: boolean;

          ngDoCheck()
          this.timeDiff(this.result.endtime, this.result.date);


          getTimeDiff(time, date)

          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(this.now.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.timeDiff = false;

          else
          this.timeDiff = true;




          In your HTML



          <span *ngIf="timeDiff"> open </span>


          ngDoCheck is a lifecycle hook (like ngOnInit) that can be summed up by




          Function that detect changes that are not tracked by Angular








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 14:33

























          answered Nov 14 '18 at 11:55









          trichetrichetrichetriche

          28.3k42560




          28.3k42560












          • Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

            – Harsimer
            Nov 14 '18 at 12:10






          • 1





            The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

            – trichetriche
            Nov 14 '18 at 12:11












          • trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

            – Harsimer
            Nov 14 '18 at 14:16











          • @Simer Sure, I have edited my answer, feel free to try it out

            – trichetriche
            Nov 14 '18 at 14:33

















          • Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

            – Harsimer
            Nov 14 '18 at 12:10






          • 1





            The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

            – trichetriche
            Nov 14 '18 at 12:11












          • trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

            – Harsimer
            Nov 14 '18 at 14:16











          • @Simer Sure, I have edited my answer, feel free to try it out

            – trichetriche
            Nov 14 '18 at 14:33
















          Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

          – Harsimer
          Nov 14 '18 at 12:10





          Thanks trichetriche. I was very confused due to multiple log. Now I have shifted private now = new Date() to scope of component but still get console several times. Though error is gone.

          – Harsimer
          Nov 14 '18 at 12:10




          1




          1





          The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

          – trichetriche
          Nov 14 '18 at 12:11






          The console log will always appear, I have given you the way to avoid the lifecycle error, not to change the Angular lifecycle itself :) And your syntax (component member) and mine (constructor parameter with access modifier) are the same, it's just that mine is a shortcut, so use the one you prefer, it won't change much !

          – trichetriche
          Nov 14 '18 at 12:11














          trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

          – Harsimer
          Nov 14 '18 at 14:16





          trichetriche Can I do something so that getTimeDiff function does not keep on executing. As I have to add some more logic to it , like setting up some flag true and as it keeps on executing again and again that flags again set to true even if I have later set it to false. This does not happen with other functions which does not involve time. Thanks.

          – Harsimer
          Nov 14 '18 at 14:16













          @Simer Sure, I have edited my answer, feel free to try it out

          – trichetriche
          Nov 14 '18 at 14:33





          @Simer Sure, I have edited my answer, feel free to try it out

          – trichetriche
          Nov 14 '18 at 14:33













          1














          Angular runs change detection and when its find that some values which has been passed to the child component has been changed Angular throws the error ExpressionChangedAfterItHasBeenCheckedError.



          Better to create one variable to hold the information rather than calling the same function on each change detection



          different = false; //<-- hold the difference state.

          getTimeDiff(time, date)


          const dateNow = new Date()
          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.different = false //<-- change to false.

          else
          this.different = true //<-- change to true.




          html



          <span *ngIf="different"> open </span>



          Note : Do not forget to call getTimeDiff function in appropriate place like ngOnInit if you want to get one time .







          share|improve this answer

























          • I have to call getTimeDiff in *ngFor. Let me update my question.

            – Harsimer
            Nov 14 '18 at 11:56






          • 1





            With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

            – trichetriche
            Nov 14 '18 at 12:44















          1














          Angular runs change detection and when its find that some values which has been passed to the child component has been changed Angular throws the error ExpressionChangedAfterItHasBeenCheckedError.



          Better to create one variable to hold the information rather than calling the same function on each change detection



          different = false; //<-- hold the difference state.

          getTimeDiff(time, date)


          const dateNow = new Date()
          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.different = false //<-- change to false.

          else
          this.different = true //<-- change to true.




          html



          <span *ngIf="different"> open </span>



          Note : Do not forget to call getTimeDiff function in appropriate place like ngOnInit if you want to get one time .







          share|improve this answer

























          • I have to call getTimeDiff in *ngFor. Let me update my question.

            – Harsimer
            Nov 14 '18 at 11:56






          • 1





            With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

            – trichetriche
            Nov 14 '18 at 12:44













          1












          1








          1







          Angular runs change detection and when its find that some values which has been passed to the child component has been changed Angular throws the error ExpressionChangedAfterItHasBeenCheckedError.



          Better to create one variable to hold the information rather than calling the same function on each change detection



          different = false; //<-- hold the difference state.

          getTimeDiff(time, date)


          const dateNow = new Date()
          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.different = false //<-- change to false.

          else
          this.different = true //<-- change to true.




          html



          <span *ngIf="different"> open </span>



          Note : Do not forget to call getTimeDiff function in appropriate place like ngOnInit if you want to get one time .







          share|improve this answer















          Angular runs change detection and when its find that some values which has been passed to the child component has been changed Angular throws the error ExpressionChangedAfterItHasBeenCheckedError.



          Better to create one variable to hold the information rather than calling the same function on each change detection



          different = false; //<-- hold the difference state.

          getTimeDiff(time, date)


          const dateNow = new Date()
          const slotDate = new Date(`$date $time`); // say time = '10:30:00' and date = '2018-11-14'

          const diff = Math.abs(Math.abs(dateNow.getTime() - slotDate.getTime()) / 3600000)

          //if diff is not negative
          if(diff)
          this.different = false //<-- change to false.

          else
          this.different = true //<-- change to true.




          html



          <span *ngIf="different"> open </span>



          Note : Do not forget to call getTimeDiff function in appropriate place like ngOnInit if you want to get one time .








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 14 '18 at 11:57

























          answered Nov 14 '18 at 11:54









          Sunil SinghSunil Singh

          6,3872628




          6,3872628












          • I have to call getTimeDiff in *ngFor. Let me update my question.

            – Harsimer
            Nov 14 '18 at 11:56






          • 1





            With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

            – trichetriche
            Nov 14 '18 at 12:44

















          • I have to call getTimeDiff in *ngFor. Let me update my question.

            – Harsimer
            Nov 14 '18 at 11:56






          • 1





            With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

            – trichetriche
            Nov 14 '18 at 12:44
















          I have to call getTimeDiff in *ngFor. Let me update my question.

          – Harsimer
          Nov 14 '18 at 11:56





          I have to call getTimeDiff in *ngFor. Let me update my question.

          – Harsimer
          Nov 14 '18 at 11:56




          1




          1





          With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

          – trichetriche
          Nov 14 '18 at 12:44





          With this, the error will not disappear, as the process is the same : the dateNow will get updated at every millisecond, resulting in the same error encountered by OP.

          – trichetriche
          Nov 14 '18 at 12:44

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53299597%2fhow-to-get-difference-in-given-time-and-current-time-in-typescript-is%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo