Fortunately, AppGini ships with the awesome Moment.JS library (
https://momentjs.com/) which helps us on date-/datetime-formatting and -conversion between formats (database format/UI/locales). Additionally the moment.js functions are tested extremely good and very readable. They will work in most cultures with different language-/locale-settings by default.
Here is a good readable example with many comments which should help not only me and you but probably many here:
Code: Select all
// file: hooks/TABLENAME-tv.js
jQuery(document).ready(function() {
// configure table and column names according to your needs
const tn = "projects"; // table name
const cn = "created_on"; // column name
// Get date format configured in AppGini project settings
const d_format = AppGini.datetimeFormat('d');
// Reading configured date format instead of hardcoding
// will help you when migrating the project to different culture in the future
// get today as moment-date
const reference_date = moment(); // today
// If you need a different date for comparison than today,
// you can use the moment constructor like this:
// Just adjust your date-value AND your initial date-format
// reference_date = moment("31.12.2023", "DD.MM.YYYY");
// let moment.js calculate yesterdays date. Good readable, I think
const yesterday = reference_date.subtract(1, 'day');
// now iterate through all cells for the given column 'cn' of table 'tn'
// we don't need the table-header's <th>'s, just the table-body's <td>'s
jQuery(`td.${tn}-${cn}`).each(function(i, e) {
const td = jQuery(e); // current cell (jQuery DOM element)
const td_text = td.text(); // text of current cell (string)
const td_datevalue = new moment(td_text, d_format); // converted string to date
// this should skip empty cells or cells with invalid contents
if (td_datevalue.isValid()) {
if (td_datevalue.isSame(reference_date, 'day'))
td.closest("tr").addClass("success");
else if (td_datevalue.isSame(yesterday, 'day'))
td.closest("tr").addClass("warning");
else if (td_datevalue.isBefore(yesterday, 'day'))
td.closest("tr").addClass("danger");
}
});
});
PS: If there is anything not working as expected, I recommend checking console for errors and logging to console. I got used to always check the actual values instead of working with unknown data further on.
console.log(YOUR_VARIABLE_NAME)
; is one of my strongest helpers.
Kind regards,
<js />
My AppGini Blog:
https://appgini.bizzworxx.de/blog
You can help us helping you:
Please always put code fragments inside
[code]...[/code]
blocks for better readability
AppGini 24.10 Revision 1579 + all AppGini Helper tools