function getAge(dateString) { var now = new Date(); var today = new Date(now.getYear(), now.getMonth(), now.getDate()); var yearNow = now.getYear(); var monthNow = now.getMonth(); var dateNow = now.getDate(); // var dob = new Date(dateString.substring(6, 10), // dateString.substring(0, 2) - 1, // dateString.substring(3, 5) // ); var dob = new Date(dateString); // This allows for use of any date format var yearDob = dob.getYear(); var monthDob = dob.getMonth(); var dateDob = dob.getDate(); var age = {}; var ageString = ""; var yearString = ""; var monthString = ""; var dayString = ""; yearAge = yearNow - yearDob; if (monthNow >= monthDob) var monthAge = monthNow - monthDob; else { yearAge--; var monthAge = 12 + monthNow - monthDob; } if (dateNow >= dateDob) var dateAge = dateNow - dateDob; else { monthAge--; var dateAge = 31 + dateNow - dateDob; if (monthAge < 0) { monthAge = 11; yearAge--; } } age = { years: yearAge, months: monthAge, days: dateAge }; if (age.years > 1) yearString = " years"; else yearString = " year"; if (age.months > 1) monthString = " months"; else monthString = " month"; if (age.days > 1) dayString = " days"; else dayString = " day"; if ((age.years > 0) && (age.months > 0) && (age.days > 0)) ageString = age.years + yearString + ", " + age.months + monthString + ", and " + age.days + dayString + " old."; else if ((age.years == 0) && (age.months == 0) && (age.days > 0)) ageString = "Only " + age.days + dayString + " old!"; else if ((age.years > 0) && (age.months == 0) && (age.days == 0)) ageString = age.years + yearString + " old. Happy Birthday!!"; else if ((age.years > 0) && (age.months > 0) && (age.days == 0)) ageString = age.years + yearString + " and " + age.months + monthString + " old."; else if ((age.years == 0) && (age.months > 0) && (age.days > 0)) ageString = age.months + monthString + " and " + age.days + dayString + " old."; else if ((age.years > 0) && (age.months == 0) && (age.days > 0)) ageString = age.years + yearString + " and " + age.days + dayString + " old."; else if ((age.years == 0) && (age.months > 0) && (age.days == 0)) ageString = age.months + monthString + " old."; else ageString = "Error"; return ageString; } /** * Quick and simple export target #table_id into a csv * * Ref: https://stackoverflow.com/questions/15547198/export-html-table-to-csv/56370447 * * @param table_id The id assigned to the html table * @param title The report title * @param separator Optional delimeter with a comma default for csv */ function download_table_as_csv(table_id, title, separator = ',') { // Select rows from table_id let rows = document.querySelectorAll('table#' + table_id + ' tr'); // Construct csv let csv = []; for (let i = 0; i < rows.length; i++) { let cols = rows[i].querySelectorAll('td, th'); let row_csv = csv_from_cols(cols, separator); csv.push(row_csv); } let csv_string = csv.join('\n'); // Download it let filename = title + ' - ' + new Date().toLocaleDateString() + '.csv'; let link = document.createElement('a'); link.style.display = 'none'; link.setAttribute('target', '_blank'); link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_string)); link.setAttribute('download', filename); document.body.appendChild(link); link.click(); document.body.removeChild(link); } /** * Quick and simple html print call * * @param divToPrint The id assigned to the div */ function print_div(divToPrint) { let myDiv = document.getElementById(divToPrint); // Hide DataTable buttons [].forEach.call(myDiv.querySelectorAll('.dt-buttons'), function (el) { el.style.visibility = 'hidden'; }); // Hide DataTable search box [].forEach.call(myDiv.querySelectorAll('.dataTables_filter'), function (el) { el.style.visibility = 'hidden'; }); // Hide DataTable pagination header [].forEach.call(myDiv.querySelectorAll('.dataTables_info'), function (el) { el.style.visibility = 'hidden'; }); // Hide DataTable pagination buttons [].forEach.call(myDiv.querySelectorAll('.dataTables_paginate'), function (el) { el.style.visibility = 'hidden'; }); let newWindow = window.open('', 'SecondWindow', 'toolbar=0,stat=0'); newWindow.document.write("" + myDiv.innerHTML + ""); newWindow.document.close(); return false; } /** * Quick and simple export target #table_ids into a csv * * Ref: https://stackoverflow.com/questions/15547198/export-html-table-to-csv/56370447 * * @param table_ids The list of ids assigned to the html tables * @param title The report title * @param separator Optional delimeter with a comma default for csv */ function download_tables_as_csv(table_ids, title, separator = ',') { let csv_strings = ""; for (let i = 0; i < table_ids.length; i++) { let table_id = table_ids[i]; // Select rows from table_id let rows = document.querySelectorAll('table#' + table_id + ' tr'); // Construct csv let csv = []; for (let i = 0; i < rows.length; i++) { let cols = rows[i].querySelectorAll('td, th'); let row_csv = csv_from_cols(cols, separator); csv.push(row_csv); } let csv_string = csv.join('\n'); csv_strings += csv_string; csv_strings += "\n\n"; } // Download it let filename = title + ' - ' + new Date().toLocaleDateString() + '.csv'; let link = document.createElement('a'); link.style.display = 'none'; link.setAttribute('target', '_blank'); link.setAttribute('href', 'data:text/csv;charset=utf-8,' + encodeURIComponent(csv_strings)); link.setAttribute('download', filename); document.body.appendChild(link); link.click(); document.body.removeChild(link); } /** * Convert column data into a csv * * @param cols Html table columns assigned * @param separator Optional delimeter with a comma default for csv */ function csv_from_cols(cols, separator) { let row = []; for (let j = 0; j < cols.length; j++) { // Clean innertext to remove multiple spaces and jumpline (break csv) let data = cols[j].innerText.replace(/(\r\n|\n|\r)/gm, '').replace(/(\s\s)/gm, ' ') // Escape double-quote with double-double-quote (see https://stackoverflow.com/questions/17808511/properly-escape-a-double-quote-in-csv) data = data.replace(/"/g, '""'); // Push escaped string row.push('"' + data + '"'); } return row.join(separator); } // JS number formatter based on https://stackoverflow.com/a/34141813/9788442 function number_format(number, decimals, dec_point, thousands_sep) { // Strip all characters but numerical ones. number = (number + '').replace(/[^0-9+\-Ee.]/g, ''); var n = !isFinite(+number) ? 0 : +number, prec = !isFinite(+decimals) ? 0 : Math.abs(decimals), sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep, dec = (typeof dec_point === 'undefined') ? '.' : dec_point, s = '', toFixedFix = function (n, prec) { var k = Math.pow(10, prec); return '' + Math.round(n * k) / k; }; // Fix for IE parseFloat(0.55).toFixed(0) = 0; s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.'); if (s[0].length > 3) { s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep); } if ((s[1] || '').length < prec) { s[1] = s[1] || ''; s[1] += new Array(prec - s[1].length + 1).join('0'); } return s.join(dec); } // will return a currency formating eg 55,000 UGX function ugandan_shillings(value, currency_code) { if (value == "N/A" || value == "") { value = 0; } else if (typeof value == "string") { value = parseInt(value); value = number_format(value, 0, ".", ","); } else if (typeof value == "number") { value = number_format(value, 0, ".", ","); } else { value = 0; } return value + " " + currency_code; }