{"id":1162,"date":"2024-12-26T12:28:30","date_gmt":"2024-12-26T06:28:30","guid":{"rendered":"https:\/\/ashrafimahbub.com\/?p=1162"},"modified":"2024-12-26T12:52:46","modified_gmt":"2024-12-26T06:52:46","slug":"day-calculator-duration-between-twodays","status":"publish","type":"post","link":"https:\/\/ashrafimahbub.com\/?p=1162","title":{"rendered":"Days Calculator: Days Between Two Dates"},"content":{"rendered":"\n<div id=\"date-calculator\">\n  <h2>Days Calculator<\/h2>\n  <label for=\"start-date\">Start Date:<\/label>\n  <input type=\"date\" id=\"start-date\">\n  <label for=\"end-date\">End Date:<\/label>\n  <input type=\"date\" id=\"end-date\">\n\n  <h3>Weekend Selection<\/h3>\n  <div id=\"weekend-selection\">\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"0\"> Sunday<\/label>\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"1\"> Monday<\/label>\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"2\"> Tuesday<\/label>\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"3\"> Wednesday<\/label>\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"4\"> Thursday<\/label>\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"5\"> Friday<\/label>\n    <label><input type=\"checkbox\" class=\"weekend\" value=\"6\"> Saturday<\/label>\n  <\/div>\n\n  <h3>Custom Holidays<\/h3>\n  <div id=\"holidays\">\n    <input type=\"date\" class=\"holiday\">\n  <\/div>\n  <button type=\"button\" id=\"add-holiday\">Add Holiday<\/button>\n\n  <button type=\"button\" id=\"calculate\">Calculate<\/button>\n\n  <h3>Results<\/h3>\n  <p>Total Days: <span id=\"total-days\">0<\/span><\/p>\n  <p>Workdays (Excluding Weekends and Holidays): <span id=\"workdays\">0<\/span><\/p>\n  <p>Months: <span id=\"months\">0<\/span><\/p>\n  <p>Years: <span id=\"years\">0<\/span><\/p>\n  <p>Hours: <span id=\"hours\">0<\/span><\/p>\n  <p>Minutes: <span id=\"minutes\">0<\/span><\/p>\n  <p>Seconds: <span id=\"seconds\">0<\/span><\/p>\n<\/div>\n\n<style>\n  #date-calculator {\n    font-family: Arial, sans-serif;\n    max-width: 400px;\n    margin: auto;\n    padding: 20px;\n    border: 1px solid #ccc;\n    border-radius: 10px;\n    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);\n  }\n  #date-calculator h2, h3 {\n    margin-bottom: 10px;\n  }\n  #date-calculator label {\n    margin-bottom: 5px;\n  }\n  #date-calculator input, #date-calculator button {\n    margin-bottom: 10px;\n    padding: 8px;\n    width: 100%;\n    box-sizing: border-box;\n  }\n  #date-calculator button {\n    background-color: #007BFF;\n    color: white;\n    border: none;\n    cursor: pointer;\n  }\n  #date-calculator button:hover {\n    background-color: #0056b3;\n  }\n  #date-calculator p {\n    font-weight: bold;\n  }\n  #weekend-selection {\n    display: flex;\n    flex-wrap: wrap;\n    gap: 10px;\n  }\n  #weekend-selection label {\n    display: flex;\n    align-items: center;\n    gap: 5px;\n    width: 48%;\n  }\n<\/style>\n\n<script>\n  document.getElementById('add-holiday').addEventListener('click', function () {\n    const holidayInput = document.createElement('input');\n    holidayInput.type = 'date';\n    holidayInput.className = 'holiday';\n    document.getElementById('holidays').appendChild(holidayInput);\n  });\n\n  document.getElementById('calculate').addEventListener('click', function () {\n    const startDate = new Date(document.getElementById('start-date').value);\n    const endDate = new Date(document.getElementById('end-date').value);\n\n    if (isNaN(startDate) || isNaN(endDate) || startDate > endDate) {\n      alert('Please enter valid dates.');\n      return;\n    }\n\n    const weekends = Array.from(document.querySelectorAll('.weekend:checked')).map(el => parseInt(el.value));\n    const holidays = Array.from(document.querySelectorAll('.holiday')).map(el => new Date(el.value)).filter(d => !isNaN(d));\n\n    let totalDays = 0;\n    let workdays = 0;\n\n    for (let d = new Date(startDate); d <= endDate; d.setDate(d.getDate() + 1)) {\n      totalDays++;\n\n      const dayOfWeek = d.getDay();\n      const isHoliday = holidays.some(holiday => holiday.getTime() === d.getTime());\n\n      if (!weekends.includes(dayOfWeek) && !isHoliday) {\n        workdays++;\n      }\n    }\n\n    const timeDiff = endDate - startDate;\n    const months = Math.floor(totalDays \/ 30.44); \/\/ Average days in a month\n    const years = Math.floor(totalDays \/ 365.25); \/\/ Average days in a year\n    const hours = Math.floor(timeDiff \/ (1000 * 60 * 60));\n    const minutes = Math.floor(timeDiff \/ (1000 * 60));\n    const seconds = Math.floor(timeDiff \/ 1000);\n\n    document.getElementById('total-days').textContent = totalDays;\n    document.getElementById('workdays').textContent = workdays;\n    document.getElementById('months').textContent = months;\n    document.getElementById('years').textContent = years;\n    document.getElementById('hours').textContent = hours;\n    document.getElementById('minutes').textContent = minutes;\n    document.getElementById('seconds').textContent = seconds;\n  });\n<\/script>\n\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=1162\"\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>Days Calculator Start Date: End Date: Weekend Selection Sunday Monday Tuesday Wednesday Thursday Friday Saturday Custom Holidays Add Holiday Calculate Results Total Days: 0 Workdays (Excluding Weekends and Holidays): 0 Months: 0 Years: 0 Hours: 0 Minutes: 0 Seconds: 0<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[263],"tags":[304],"class_list":["post-1162","post","type-post","status-publish","format-standard","hentry","category-hr-analytics","tag-days-calculator-days-between-two-dates"],"_links":{"self":[{"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts\/1162","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=1162"}],"version-history":[{"count":16,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts\/1162\/revisions"}],"predecessor-version":[{"id":1178,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=\/wp\/v2\/posts\/1162\/revisions\/1178"}],"wp:attachment":[{"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ashrafimahbub.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}