Fuzzy Humanize date using Moment.js for datatable searching The 2019 Stack Overflow Developer Survey Results Are InFinding image by dateDate formatter for javascriptApproximating Date for a D3.js timelineDate Comparison ScriptConfiguring a date picker with support for disabled datesAdd WeekDays to DateBuilding a 2D array “month view” for a given dateHumanDate for easier date calculationsDate class for Budgeting AppCompute end date for project
Does it makes sense to buy a new cycle to learn riding?
Is there a general name for the setup in which payoffs are not known exactly but players try to influence each other's perception of the payoffs?
How is the Antim Sanskar of married Hindu women done?
Why do some words that are not inflected have an umlaut?
Is it true that “A.D.” is traditionally placed before the year number?
aging parents with no investments
Unbreakable Formation vs. Cry of the Carnarium
Is this food a bread or a loaf?
Understanding the implication of what "well-defined" means for the operation in quotient group
What is the best strategy for white in this position?
How was Skylab's orbit inclination chosen?
Patience, young "Padovan"
When to use the root test. Is this not a good situation to use it?
Should I use my personal or workplace e-mail when registering to external websites for work purpose?
Inflated grade on resume at previous job, might former employer tell new employer?
What does Linus Torvalds mean when he says that Git "never ever" tracks a file?
Why isn't airport relocation done gradually?
Is "plugging out" electronic devices an American expression?
How can I fix this gap between bookcases I made?
Is domain driven design an anti-SQL pattern?
What is the use of option -o in the useradd command?
Where does the "burst of radiance" from Holy Weapon originate?
Why don't Unix/Linux systems traverse through directories until they find the required version of a linked library?
What could be the right powersource for 15 seconds lifespan disposable giant chainsaw?
Fuzzy Humanize date using Moment.js for datatable searching
The 2019 Stack Overflow Developer Survey Results Are InFinding image by dateDate formatter for javascriptApproximating Date for a D3.js timelineDate Comparison ScriptConfiguring a date picker with support for disabled datesAdd WeekDays to DateBuilding a 2D array “month view” for a given dateHumanDate for easier date calculationsDate class for Budgeting AppCompute end date for project
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
$begingroup$
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
javascript datetime
$endgroup$
bumped to the homepage by Community♦ 2 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
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
javascript datetime
$endgroup$
bumped to the homepage by Community♦ 2 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
$begingroup$
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
javascript datetime
$endgroup$
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>
javascript datetime
javascript datetime
asked Jul 12 '18 at 10:20
AdarshAdarsh
1496
1496
bumped to the homepage by Community♦ 2 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 2 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$begingroup$
What if you have three functions that each calculate the string on its own? Therefore:
function days_diff(daydiff)
if (daydiff==0) return 'Today'
if (daydiff==1) return 'Tomorrow'
if (daydiff==-1) return 'Yesterday'
return null
function weeks_diff(daydiff) {
if (daydiff > 0 && daydiff < 7) return 'Thisweek'
if (daydiff < 7) return 'Nextweek'
return null
...
Then you can have your one aggregate function that calls the three of them...
Here's the much more complicated one from Rails that might provide guidance/inspiration: https://github.com/rails/rails/blob/2c97fbf6503c9199f3fe5ed06222e7226dc6fcd9/actionview/lib/action_view/helpers/date_helper.rb#L104
$endgroup$
add a comment |
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',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
What if you have three functions that each calculate the string on its own? Therefore:
function days_diff(daydiff)
if (daydiff==0) return 'Today'
if (daydiff==1) return 'Tomorrow'
if (daydiff==-1) return 'Yesterday'
return null
function weeks_diff(daydiff) {
if (daydiff > 0 && daydiff < 7) return 'Thisweek'
if (daydiff < 7) return 'Nextweek'
return null
...
Then you can have your one aggregate function that calls the three of them...
Here's the much more complicated one from Rails that might provide guidance/inspiration: https://github.com/rails/rails/blob/2c97fbf6503c9199f3fe5ed06222e7226dc6fcd9/actionview/lib/action_view/helpers/date_helper.rb#L104
$endgroup$
add a comment |
$begingroup$
What if you have three functions that each calculate the string on its own? Therefore:
function days_diff(daydiff)
if (daydiff==0) return 'Today'
if (daydiff==1) return 'Tomorrow'
if (daydiff==-1) return 'Yesterday'
return null
function weeks_diff(daydiff) {
if (daydiff > 0 && daydiff < 7) return 'Thisweek'
if (daydiff < 7) return 'Nextweek'
return null
...
Then you can have your one aggregate function that calls the three of them...
Here's the much more complicated one from Rails that might provide guidance/inspiration: https://github.com/rails/rails/blob/2c97fbf6503c9199f3fe5ed06222e7226dc6fcd9/actionview/lib/action_view/helpers/date_helper.rb#L104
$endgroup$
add a comment |
$begingroup$
What if you have three functions that each calculate the string on its own? Therefore:
function days_diff(daydiff)
if (daydiff==0) return 'Today'
if (daydiff==1) return 'Tomorrow'
if (daydiff==-1) return 'Yesterday'
return null
function weeks_diff(daydiff) {
if (daydiff > 0 && daydiff < 7) return 'Thisweek'
if (daydiff < 7) return 'Nextweek'
return null
...
Then you can have your one aggregate function that calls the three of them...
Here's the much more complicated one from Rails that might provide guidance/inspiration: https://github.com/rails/rails/blob/2c97fbf6503c9199f3fe5ed06222e7226dc6fcd9/actionview/lib/action_view/helpers/date_helper.rb#L104
$endgroup$
What if you have three functions that each calculate the string on its own? Therefore:
function days_diff(daydiff)
if (daydiff==0) return 'Today'
if (daydiff==1) return 'Tomorrow'
if (daydiff==-1) return 'Yesterday'
return null
function weeks_diff(daydiff) {
if (daydiff > 0 && daydiff < 7) return 'Thisweek'
if (daydiff < 7) return 'Nextweek'
return null
...
Then you can have your one aggregate function that calls the three of them...
Here's the much more complicated one from Rails that might provide guidance/inspiration: https://github.com/rails/rails/blob/2c97fbf6503c9199f3fe5ed06222e7226dc6fcd9/actionview/lib/action_view/helpers/date_helper.rb#L104
answered Aug 13 '18 at 4:00
ndpndp
1,21686
1,21686
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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