Fuzzy Humanize date using Moment.js for datatable searching

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












In my JS project, I am using datatable plugin to show data where I have a date column which users can filter/search.



In order to help users to search using a logical string such as today, thismonth , lastweek I am writting a fucntion which uses MomentJs.



I am looking at the most efficient way of writing this fuzzyDate function.






function fuzzyDate(mydate)
var fuzzy1 = ''; var fuzzy2 = ''; var fuzzy3 = '';

var todaydt = moment(moment().format('YYYY-MM-DD'));
var daydiff = mydate.diff(todaydt,'day');
if (daydiff==-1)fuzzy1='Yesterday'
if (daydiff==0)fuzzy1='Today'
if (daydiff==1)fuzzy1='Tomorrow'

var weekdiff = mydate.diff(todaydt,'week');
if (weekdiff==-1)fuzzy2='LastWeek'
if (weekdiff==0)fuzzy2='ThisWeek'
if (weekdiff==1)fuzzy2='NextWeek'

var monthdiff = mydate.diff(todaydt,'month');
if (monthdiff==-1)fuzzy3='LastMonth'
if (monthdiff==0)fuzzy3='ThisMonth'
if (monthdiff==1)fuzzy3='NextMonth'

return fuzzy1 + ' ' + fuzzy2 + ' ' + fuzzy3;


console.log( '2018-06-01 - ', fuzzyDate(moment('2018-06-01','YYYY-MM-DD')) ) ;
console.log( '2018-07-01 - ', fuzzyDate(moment('2018-07-01','YYYY-MM-DD')) ) ;
console.log( '2018-07-07 - ', fuzzyDate(moment('2018-07-07','YYYY-MM-DD')) ) ;
console.log( '2018-07-10 - ', fuzzyDate(moment('2018-07-10','YYYY-MM-DD')) ) ;
console.log( '2018-07-11 - ', fuzzyDate(moment('2018-07-11','YYYY-MM-DD')) ) ;
console.log( '2018-07-12 - ', fuzzyDate(moment('2018-07-12','YYYY-MM-DD')) ) ;
console.log( '2018-07-13 - ', fuzzyDate(moment('2018-07-13','YYYY-MM-DD')) ) ;
console.log( '2018-07-15 - ', fuzzyDate(moment('2018-07-15','YYYY-MM-DD')) ) ;
console.log( '2018-07-30 - ', fuzzyDate(moment('2018-07-30','YYYY-MM-DD')) ) ;
console.log( '2018-08-30 - ', fuzzyDate(moment('2018-08-30','YYYY-MM-DD')) ) ;

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





Output of the above function



2018-06-01 - LastMonth
2018-07-01 - LastWeek ThisMonth
2018-07-07 - ThisWeek ThisMonth
2018-07-10 - ThisWeek ThisMonth
2018-07-11 - Yesterday ThisWeek ThisMonth
2018-07-12 - Today ThisWeek ThisMonth
2018-07-13 - Tomorrow ThisWeek ThisMonth
2018-07-15 - ThisWeek ThisMonth
2018-07-30 - ThisMonth
2018-08-30 - NextMonth






share|improve this question

























    up vote
    1
    down vote

    favorite












    In my JS project, I am using datatable plugin to show data where I have a date column which users can filter/search.



    In order to help users to search using a logical string such as today, thismonth , lastweek I am writting a fucntion which uses MomentJs.



    I am looking at the most efficient way of writing this fuzzyDate function.






    function fuzzyDate(mydate)
    var fuzzy1 = ''; var fuzzy2 = ''; var fuzzy3 = '';

    var todaydt = moment(moment().format('YYYY-MM-DD'));
    var daydiff = mydate.diff(todaydt,'day');
    if (daydiff==-1)fuzzy1='Yesterday'
    if (daydiff==0)fuzzy1='Today'
    if (daydiff==1)fuzzy1='Tomorrow'

    var weekdiff = mydate.diff(todaydt,'week');
    if (weekdiff==-1)fuzzy2='LastWeek'
    if (weekdiff==0)fuzzy2='ThisWeek'
    if (weekdiff==1)fuzzy2='NextWeek'

    var monthdiff = mydate.diff(todaydt,'month');
    if (monthdiff==-1)fuzzy3='LastMonth'
    if (monthdiff==0)fuzzy3='ThisMonth'
    if (monthdiff==1)fuzzy3='NextMonth'

    return fuzzy1 + ' ' + fuzzy2 + ' ' + fuzzy3;


    console.log( '2018-06-01 - ', fuzzyDate(moment('2018-06-01','YYYY-MM-DD')) ) ;
    console.log( '2018-07-01 - ', fuzzyDate(moment('2018-07-01','YYYY-MM-DD')) ) ;
    console.log( '2018-07-07 - ', fuzzyDate(moment('2018-07-07','YYYY-MM-DD')) ) ;
    console.log( '2018-07-10 - ', fuzzyDate(moment('2018-07-10','YYYY-MM-DD')) ) ;
    console.log( '2018-07-11 - ', fuzzyDate(moment('2018-07-11','YYYY-MM-DD')) ) ;
    console.log( '2018-07-12 - ', fuzzyDate(moment('2018-07-12','YYYY-MM-DD')) ) ;
    console.log( '2018-07-13 - ', fuzzyDate(moment('2018-07-13','YYYY-MM-DD')) ) ;
    console.log( '2018-07-15 - ', fuzzyDate(moment('2018-07-15','YYYY-MM-DD')) ) ;
    console.log( '2018-07-30 - ', fuzzyDate(moment('2018-07-30','YYYY-MM-DD')) ) ;
    console.log( '2018-08-30 - ', fuzzyDate(moment('2018-08-30','YYYY-MM-DD')) ) ;

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





    Output of the above function



    2018-06-01 - LastMonth
    2018-07-01 - LastWeek ThisMonth
    2018-07-07 - ThisWeek ThisMonth
    2018-07-10 - ThisWeek ThisMonth
    2018-07-11 - Yesterday ThisWeek ThisMonth
    2018-07-12 - Today ThisWeek ThisMonth
    2018-07-13 - Tomorrow ThisWeek ThisMonth
    2018-07-15 - ThisWeek ThisMonth
    2018-07-30 - ThisMonth
    2018-08-30 - NextMonth






    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      In my JS project, I am using datatable plugin to show data where I have a date column which users can filter/search.



      In order to help users to search using a logical string such as today, thismonth , lastweek I am writting a fucntion which uses MomentJs.



      I am looking at the most efficient way of writing this fuzzyDate function.






      function fuzzyDate(mydate)
      var fuzzy1 = ''; var fuzzy2 = ''; var fuzzy3 = '';

      var todaydt = moment(moment().format('YYYY-MM-DD'));
      var daydiff = mydate.diff(todaydt,'day');
      if (daydiff==-1)fuzzy1='Yesterday'
      if (daydiff==0)fuzzy1='Today'
      if (daydiff==1)fuzzy1='Tomorrow'

      var weekdiff = mydate.diff(todaydt,'week');
      if (weekdiff==-1)fuzzy2='LastWeek'
      if (weekdiff==0)fuzzy2='ThisWeek'
      if (weekdiff==1)fuzzy2='NextWeek'

      var monthdiff = mydate.diff(todaydt,'month');
      if (monthdiff==-1)fuzzy3='LastMonth'
      if (monthdiff==0)fuzzy3='ThisMonth'
      if (monthdiff==1)fuzzy3='NextMonth'

      return fuzzy1 + ' ' + fuzzy2 + ' ' + fuzzy3;


      console.log( '2018-06-01 - ', fuzzyDate(moment('2018-06-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-01 - ', fuzzyDate(moment('2018-07-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-07 - ', fuzzyDate(moment('2018-07-07','YYYY-MM-DD')) ) ;
      console.log( '2018-07-10 - ', fuzzyDate(moment('2018-07-10','YYYY-MM-DD')) ) ;
      console.log( '2018-07-11 - ', fuzzyDate(moment('2018-07-11','YYYY-MM-DD')) ) ;
      console.log( '2018-07-12 - ', fuzzyDate(moment('2018-07-12','YYYY-MM-DD')) ) ;
      console.log( '2018-07-13 - ', fuzzyDate(moment('2018-07-13','YYYY-MM-DD')) ) ;
      console.log( '2018-07-15 - ', fuzzyDate(moment('2018-07-15','YYYY-MM-DD')) ) ;
      console.log( '2018-07-30 - ', fuzzyDate(moment('2018-07-30','YYYY-MM-DD')) ) ;
      console.log( '2018-08-30 - ', fuzzyDate(moment('2018-08-30','YYYY-MM-DD')) ) ;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





      Output of the above function



      2018-06-01 - LastMonth
      2018-07-01 - LastWeek ThisMonth
      2018-07-07 - ThisWeek ThisMonth
      2018-07-10 - ThisWeek ThisMonth
      2018-07-11 - Yesterday ThisWeek ThisMonth
      2018-07-12 - Today ThisWeek ThisMonth
      2018-07-13 - Tomorrow ThisWeek ThisMonth
      2018-07-15 - ThisWeek ThisMonth
      2018-07-30 - ThisMonth
      2018-08-30 - NextMonth






      share|improve this question











      In my JS project, I am using datatable plugin to show data where I have a date column which users can filter/search.



      In order to help users to search using a logical string such as today, thismonth , lastweek I am writting a fucntion which uses MomentJs.



      I am looking at the most efficient way of writing this fuzzyDate function.






      function fuzzyDate(mydate)
      var fuzzy1 = ''; var fuzzy2 = ''; var fuzzy3 = '';

      var todaydt = moment(moment().format('YYYY-MM-DD'));
      var daydiff = mydate.diff(todaydt,'day');
      if (daydiff==-1)fuzzy1='Yesterday'
      if (daydiff==0)fuzzy1='Today'
      if (daydiff==1)fuzzy1='Tomorrow'

      var weekdiff = mydate.diff(todaydt,'week');
      if (weekdiff==-1)fuzzy2='LastWeek'
      if (weekdiff==0)fuzzy2='ThisWeek'
      if (weekdiff==1)fuzzy2='NextWeek'

      var monthdiff = mydate.diff(todaydt,'month');
      if (monthdiff==-1)fuzzy3='LastMonth'
      if (monthdiff==0)fuzzy3='ThisMonth'
      if (monthdiff==1)fuzzy3='NextMonth'

      return fuzzy1 + ' ' + fuzzy2 + ' ' + fuzzy3;


      console.log( '2018-06-01 - ', fuzzyDate(moment('2018-06-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-01 - ', fuzzyDate(moment('2018-07-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-07 - ', fuzzyDate(moment('2018-07-07','YYYY-MM-DD')) ) ;
      console.log( '2018-07-10 - ', fuzzyDate(moment('2018-07-10','YYYY-MM-DD')) ) ;
      console.log( '2018-07-11 - ', fuzzyDate(moment('2018-07-11','YYYY-MM-DD')) ) ;
      console.log( '2018-07-12 - ', fuzzyDate(moment('2018-07-12','YYYY-MM-DD')) ) ;
      console.log( '2018-07-13 - ', fuzzyDate(moment('2018-07-13','YYYY-MM-DD')) ) ;
      console.log( '2018-07-15 - ', fuzzyDate(moment('2018-07-15','YYYY-MM-DD')) ) ;
      console.log( '2018-07-30 - ', fuzzyDate(moment('2018-07-30','YYYY-MM-DD')) ) ;
      console.log( '2018-08-30 - ', fuzzyDate(moment('2018-08-30','YYYY-MM-DD')) ) ;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





      Output of the above function



      2018-06-01 - LastMonth
      2018-07-01 - LastWeek ThisMonth
      2018-07-07 - ThisWeek ThisMonth
      2018-07-10 - ThisWeek ThisMonth
      2018-07-11 - Yesterday ThisWeek ThisMonth
      2018-07-12 - Today ThisWeek ThisMonth
      2018-07-13 - Tomorrow ThisWeek ThisMonth
      2018-07-15 - ThisWeek ThisMonth
      2018-07-30 - ThisMonth
      2018-08-30 - NextMonth





      function fuzzyDate(mydate)
      var fuzzy1 = ''; var fuzzy2 = ''; var fuzzy3 = '';

      var todaydt = moment(moment().format('YYYY-MM-DD'));
      var daydiff = mydate.diff(todaydt,'day');
      if (daydiff==-1)fuzzy1='Yesterday'
      if (daydiff==0)fuzzy1='Today'
      if (daydiff==1)fuzzy1='Tomorrow'

      var weekdiff = mydate.diff(todaydt,'week');
      if (weekdiff==-1)fuzzy2='LastWeek'
      if (weekdiff==0)fuzzy2='ThisWeek'
      if (weekdiff==1)fuzzy2='NextWeek'

      var monthdiff = mydate.diff(todaydt,'month');
      if (monthdiff==-1)fuzzy3='LastMonth'
      if (monthdiff==0)fuzzy3='ThisMonth'
      if (monthdiff==1)fuzzy3='NextMonth'

      return fuzzy1 + ' ' + fuzzy2 + ' ' + fuzzy3;


      console.log( '2018-06-01 - ', fuzzyDate(moment('2018-06-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-01 - ', fuzzyDate(moment('2018-07-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-07 - ', fuzzyDate(moment('2018-07-07','YYYY-MM-DD')) ) ;
      console.log( '2018-07-10 - ', fuzzyDate(moment('2018-07-10','YYYY-MM-DD')) ) ;
      console.log( '2018-07-11 - ', fuzzyDate(moment('2018-07-11','YYYY-MM-DD')) ) ;
      console.log( '2018-07-12 - ', fuzzyDate(moment('2018-07-12','YYYY-MM-DD')) ) ;
      console.log( '2018-07-13 - ', fuzzyDate(moment('2018-07-13','YYYY-MM-DD')) ) ;
      console.log( '2018-07-15 - ', fuzzyDate(moment('2018-07-15','YYYY-MM-DD')) ) ;
      console.log( '2018-07-30 - ', fuzzyDate(moment('2018-07-30','YYYY-MM-DD')) ) ;
      console.log( '2018-08-30 - ', fuzzyDate(moment('2018-08-30','YYYY-MM-DD')) ) ;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>





      function fuzzyDate(mydate)
      var fuzzy1 = ''; var fuzzy2 = ''; var fuzzy3 = '';

      var todaydt = moment(moment().format('YYYY-MM-DD'));
      var daydiff = mydate.diff(todaydt,'day');
      if (daydiff==-1)fuzzy1='Yesterday'
      if (daydiff==0)fuzzy1='Today'
      if (daydiff==1)fuzzy1='Tomorrow'

      var weekdiff = mydate.diff(todaydt,'week');
      if (weekdiff==-1)fuzzy2='LastWeek'
      if (weekdiff==0)fuzzy2='ThisWeek'
      if (weekdiff==1)fuzzy2='NextWeek'

      var monthdiff = mydate.diff(todaydt,'month');
      if (monthdiff==-1)fuzzy3='LastMonth'
      if (monthdiff==0)fuzzy3='ThisMonth'
      if (monthdiff==1)fuzzy3='NextMonth'

      return fuzzy1 + ' ' + fuzzy2 + ' ' + fuzzy3;


      console.log( '2018-06-01 - ', fuzzyDate(moment('2018-06-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-01 - ', fuzzyDate(moment('2018-07-01','YYYY-MM-DD')) ) ;
      console.log( '2018-07-07 - ', fuzzyDate(moment('2018-07-07','YYYY-MM-DD')) ) ;
      console.log( '2018-07-10 - ', fuzzyDate(moment('2018-07-10','YYYY-MM-DD')) ) ;
      console.log( '2018-07-11 - ', fuzzyDate(moment('2018-07-11','YYYY-MM-DD')) ) ;
      console.log( '2018-07-12 - ', fuzzyDate(moment('2018-07-12','YYYY-MM-DD')) ) ;
      console.log( '2018-07-13 - ', fuzzyDate(moment('2018-07-13','YYYY-MM-DD')) ) ;
      console.log( '2018-07-15 - ', fuzzyDate(moment('2018-07-15','YYYY-MM-DD')) ) ;
      console.log( '2018-07-30 - ', fuzzyDate(moment('2018-07-30','YYYY-MM-DD')) ) ;
      console.log( '2018-08-30 - ', fuzzyDate(moment('2018-08-30','YYYY-MM-DD')) ) ;

      <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>








      share|improve this question










      share|improve this question




      share|improve this question









      asked Jul 12 at 10:20









      Adarsh

      1464




      1464

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198352%2ffuzzy-humanize-date-using-moment-js-for-datatable-searching%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f198352%2ffuzzy-humanize-date-using-moment-js-for-datatable-searching%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?