{"id":1208,"date":"2024-12-31T16:40:52","date_gmt":"2024-12-31T10:40:52","guid":{"rendered":"https:\/\/ashrafimahbub.com\/?p=1208"},"modified":"2024-12-31T17:04:36","modified_gmt":"2024-12-31T11:04:36","slug":"employee-service-lenght-calculator-entering-employee-informaiton-and-calculation-and-export","status":"publish","type":"post","link":"https:\/\/ashrafimahbub.com\/?p=1208","title":{"rendered":"Employee Service Lenght Calculator &#8211; Entering Employee Informaiton, Calculation and Export"},"content":{"rendered":"\n<!DOCTYPE html>\n<html lang=\"en\">\n<head>\n    <meta charset=\"UTF-8\">\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n    <title>Service Length Calculator<\/title>\n    <script src=\"https:\/\/cdnjs.cloudflare.com\/ajax\/libs\/xlsx\/0.18.5\/xlsx.full.min.js\"><\/script>\n    <style>\n        body {\n            font-family: Arial, sans-serif;\n            padding: 20px;\n            margin: 0;\n        }\n\n        h1 {\n            text-align: center;\n        }\n\n        .input-table {\n            margin: 10px 0;\n            border-collapse: collapse;\n            width: 100%;\n        }\n\n        .input-table th, .input-table td {\n            border: 1px solid #ddd;\n            padding: 10px;\n            text-align: center;\n        }\n\n        .input-table input {\n            width: 100%;\n            border: none;\n            text-align: center;\n            font-size: 16px;\n        }\n\n        button {\n            padding: 10px;\n            font-size: 16px;\n            margin: 5px 0;\n        }\n\n        table {\n            width: 100%;\n            margin-top: 20px;\n            border-collapse: collapse;\n        }\n\n        table, th, td {\n            border: 1px solid #ddd;\n            text-align: left;\n        }\n\n        th, td {\n            padding: 10px;\n        }\n    <\/style>\n<\/head>\n<body>\n    <h1>Service Length Calculator<\/h1>\n\n    <table class=\"input-table\" id=\"inputTable\">\n        <thead>\n            <tr>\n                <th>Employee ID<\/th>\n                <th>Joining Date (DD-MM-YYYY)<\/th>\n            <\/tr>\n        <\/thead>\n        <tbody>\n            <tr>\n                <td><input type=\"text\" placeholder=\"Enter Employee ID\"><\/td>\n                <td><input type=\"text\" placeholder=\"DD-MM-YYYY\"><\/td>\n            <\/tr>\n        <\/tbody>\n    <\/table>\n\n    <button id=\"addRowButton\">Add Row<\/button>\n    <label for=\"calcDate\">Calculate service length as of:<\/label>\n    <input type=\"date\" id=\"calcDate\">\n    <br>\n\n    <button id=\"calculateButton\">Calculate Service Length<\/button>\n    <button id=\"exportButton\" disabled>Export to Excel<\/button>\n\n    <table id=\"dataTable\">\n        <thead>\n            <tr>\n                <th>Employee ID<\/th>\n                <th>Joining Date<\/th>\n                <th>Service Length<\/th>\n            <\/tr>\n        <\/thead>\n        <tbody><\/tbody>\n    <\/table>\n\n    <script>\n        const inputTable = document.getElementById('inputTable').querySelector('tbody');\n        const calcDateInput = document.getElementById('calcDate');\n        const calculateButton = document.getElementById('calculateButton');\n        const exportButton = document.getElementById('exportButton');\n        const addRowButton = document.getElementById('addRowButton');\n        const tableBody = document.getElementById('dataTable').querySelector('tbody');\n        let employeeData = [];\n\n        addRowButton.addEventListener('click', () => {\n            const newRow = document.createElement('tr');\n            newRow.innerHTML = `\n                <td><input type=\"text\" placeholder=\"Enter Employee ID\"><\/td>\n                <td><input type=\"text\" placeholder=\"DD-MM-YYYY\"><\/td>\n            `;\n            inputTable.appendChild(newRow);\n        });\n\n        calculateButton.addEventListener('click', calculateServiceLength);\n        exportButton.addEventListener('click', exportToExcel);\n\n        function parseDate(dateStr) {\n            const [day, month, year] = dateStr.split('-').map(Number);\n            return new Date(year, month - 1, day);\n        }\n\n        function calculateServiceLength() {\n            const calcDateStr = calcDateInput.value;\n            if (!calcDateStr) {\n                alert('Please enter a calculation date.');\n                return;\n            }\n\n            const calcDate = new Date(calcDateStr);\n            if (isNaN(calcDate.getTime())) {\n                alert('Invalid calculation date format.');\n                return;\n            }\n\n            const rows = inputTable.querySelectorAll('tr');\n            employeeData = [];\n\n            rows.forEach(row => {\n                const idInput = row.cells[0].querySelector('input').value;\n                const dateInput = row.cells[1].querySelector('input').value;\n\n                if (idInput && dateInput) {\n                    const joiningDate = parseDate(dateInput);\n                    if (isNaN(joiningDate.getTime())) {\n                        alert(`Invalid date format for Employee ID: ${idInput}. Use DD-MM-YYYY.`);\n                        return;\n                    }\n\n                    const serviceLength = calculateLength(joiningDate, calcDate);\n                    const formattedDate = formatDate(joiningDate);\n                    employeeData.push([idInput, formattedDate, serviceLength]);\n                }\n            });\n\n            if (employeeData.length === 0) {\n                alert('Please fill in at least one row with valid data.');\n                return;\n            }\n\n            renderTable(employeeData);\n            exportButton.disabled = false;\n        }\n\n        function calculateLength(startDate, endDate) {\n            if (isNaN(startDate.getTime())) return 'Invalid Date';\n\n            let years = endDate.getFullYear() - startDate.getFullYear();\n            let months = endDate.getMonth() - startDate.getMonth();\n            let days = endDate.getDate() - startDate.getDate();\n\n            if (days < 0) {\n                months -= 1;\n                const prevMonth = new Date(endDate.getFullYear(), endDate.getMonth(), 0);\n                days += prevMonth.getDate();\n            }\n\n            if (months < 0) {\n                years -= 1;\n                months += 12;\n            }\n\n            return `${years} years, ${months} months, ${days} days`;\n        }\n\n        function formatDate(date) {\n            const day = String(date.getDate()).padStart(2, '0');\n            const month = String(date.getMonth() + 1).padStart(2, '0');\n            const year = date.getFullYear();\n            return `${day}-${month}-${year}`;\n        }\n\n        function renderTable(data) {\n            tableBody.innerHTML = '';\n\n            data.forEach(row => {\n                const tr = document.createElement('tr');\n                row.forEach(cell => {\n                    const td = document.createElement('td');\n                    td.textContent = cell;\n                    tr.appendChild(td);\n                });\n                tableBody.appendChild(tr);\n            });\n        }\n\n        function exportToExcel() {\n            const worksheet = XLSX.utils.aoa_to_sheet([['Employee ID', 'Joining Date', 'Service Length'], ...employeeData]);\n            const workbook = XLSX.utils.book_new();\n            XLSX.utils.book_append_sheet(workbook, worksheet, 'Sheet1');\n            XLSX.writeFile(workbook, 'Service_Length.xlsx');\n        }\n    <\/script>\n<\/body>\n<\/html>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\">Do you have a large data and do not want to enter the data manually? You can just upload an Excel file and do the calculation and even you can export. To do that <a href=\"https:\/\/ashrafimahbub.com\/?p=1181\">CLICK HERE.<\/a><\/p>\n<div class=\"fb-background-color\">\n\t\t\t  <div \n\t\t\t  \tclass = \"fb-comments\" \n\t\t\t  \tdata-href = \"https:\/\/ashrafimahbub.com\/?p=1208\"\n\t\t\t  \tdata-numposts = \"10\"\n\t\t\t  \tdata-lazy = \"true\"\n\t\t\t\tdata-colorscheme = \"light\"\n\t\t\t\tdata-order-by = \"social\"\n\t\t\t\tdata-mobile=true>\n\t\t\t  <\/div><\/div>\n\t\t  <style>\n\t\t    .fb-background-color {\n\t\t\t\tbackground: #ffffff !important;\n\t\t\t}\n\t\t\t.fb_iframe_widget_fluid_desktop iframe {\n\t\t\t    width: 320px !important;\n\t\t\t}\n\t\t  <\/style>\n\t\t  ","protected":false},"excerpt":{"rendered":"<p>Service Length Calculator Service Length Calculator Employee ID Joining Date (DD-MM-YYYY) Add Row Calculate service length as of: Calculate Service Length Export to Excel Employee ID Joining Date Service Length Do you have a large data and do not want to enter the data manually? You can just upload an Excel file and do the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,263],"tags":[307,306,310,317,308,316,312,314,305,311,315,309,313,319,318],"class_list":["post-1208","post","type-post","status-publish","format-standard","hentry","category-all","category-hr-analytics","tag-calculate-service-length","tag-employee-calculator","tag-employee-excel-data","tag-employee-management-tool","tag-employee-service-length","tag-employee-service-tracking","tag-excel-date-conversion","tag-export-service-length-to-excel","tag-html-service-calculator","tag-joining-date-calculation","tag-service-length-calculation-tool","tag-service-length-export","tag-wordpress-custom-html","tag-wordpress-employee-tool","tag-wordpress-table-script"],"_links":{"self":[{"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts\/1208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1208"}],"version-history":[{"count":8,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts\/1208\/revisions"}],"predecessor-version":[{"id":1225,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts\/1208\/revisions\/1225"}],"wp:attachment":[{"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}