Calculate days with stored date colomn and datetime now
Calculate days with stored date colomn and datetime now
I am trying to calculate the diference in days between a stored date in colomn 'testpay' in format 2021-03-31 and the current date, in this case the result should be 55. I have done this in php on this way:
$day1 = strtotime($row['testpay']);
$day2 = strtotime("NOW");
$diff = round((($day1 - $day2) / 86400));
I am trying to do this with datatables on this way, this does not give the right result yet. See below code and testcase.
"columns": [
{ data: null, title : 'days',
render: function ( data, type, row ) {
return ( (moment(data.testpay, 'YYYY-MM-DD HH:mm:ss')-moment())/86400);
} },
http://live.datatables.net/banurege/1/edit
This question has accepted answers - jump to:
Answers
Google is your friend for questions that aren't Datatables specific. Searching for "moment difference between two dates" finds the [moment diff docs](moment difference between two dates) first. This doc shows how to calculate the difference between two dates using moment.js.
I updated your test case with your above code snippet then changed it to use the moment diff() method:
http://live.datatables.net/banurege/3/edit
Kevin
Thanks, i guess i am allmost here. See below code, is it possible to combine the two? and when the value is 0 or below it should display 0. I forgot to mention that..
So combine the
and the
So that the day value will be colored as well?
Forget the color question from the last one. I used your answer from the previous question and that worked for this case to! Thanks !. All that is left is to know how to display a 0 when the result is <=0.
The updated code:
You can use an if statement or if you want to get fancy use a Ternary if.
Kevin
Thanks for the reaction. I know how to do it in php. Javascript is new for me. I will try, thanks !
Thanks, got it working ! This is my final code:
Looks good. Take a look at w3cschools for Javascript and HTML tutorials. The have online REPL that you can work directly with the tutorial examples.
Kevin