Friday, June 27, 2025

Count days between two dates

 Here I am explaining code for count days between two dates.





<!DOCTYPE html>


<html lang="en">

<head>

<meta charset="UTF-8">

<title>Date Difference - Years, Months, Days</title>

<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body class="p-4">




<div class="container">

<h3 class="mb-4">વાદગ્રસ્ત હુકમ તારીખ અને દાખલ તારીખ : વર્ષો, મહિનાઓ અને દિવસોની ગણતરી કરો</h3>

<div class="row mb-3">

<div class="col-md-6">

<label for="date1" class="form-label">વાદગ્રસ્ત હુકમ તારીખ (dd/mm/yyyy)</label>

<input type="text" id="date1" class="form-control" placeholder="dd/mm/yyyy">

</div>




<div class="col-md-6">

<label for="date2" class="form-label">દાખલ તારીખ (default: today)</label>

<input type="date" id="date2" class="form-control">

</div>

</div>




<div id="reultdiv"></div>

</div>




<script>

document.getElementById('date2').valueAsDate = new Date();




document.getElementById('date1').addEventListener('change', calculate);

document.getElementById('date2').addEventListener('change', calculate);




function calculate() {

document.getElementById("reultdiv").innerHTML = '';




const date1Str = document.getElementById('date1').value;

const date2Str = document.getElementById('date2').value;




const parts = date1Str.split('/');

if (parts.length !== 3) return;




const d1 = new Date(`${parts[2]}-${parts[1]}-${parts[0]}`);

const d2 = new Date(date2Str);

if (isNaN(d1.getTime()) || isNaN(d2.getTime())) return;




let output = '';




// Helper function to calculate and return HTML

function getAlertHTML(label, className, from, to) {

let totalDays = Math.floor((to - from) / (1000 * 60 * 60 * 24));

if (totalDays < 0) return ''; // skip if invalid range




let years = to.getFullYear() - from.getFullYear();

let months = to.getMonth() - from.getMonth();

let days = to.getDate() - from.getDate();




if (days < 0) {

months--;

let temp = new Date(to.getFullYear(), to.getMonth(), 0);

days += temp.getDate();

}




if (months < 0) {

years--;

months += 12;

}




return `

<div class="alert ${className}" role="alert">

<strong>${label}</strong><br>

🗓 <strong>કુલ દિવસો:</strong> ${totalDays} દિવસ<br>

📅 <strong>અવધિ:</strong> ${years} વર્ષ, ${months} મહિના, ${days} દિવસ

</div>

`;

}




// 🔹 Primary calculation

if (d1 <= d2) {

output += getAlertHTML("પ્રાથમિક ગણતરી", "alert-primary", d1, d2);

}




// 🔹 30 days check

let d30 = new Date(d2);

d30.setDate(d30.getDate() - 30);

if (d1 <= d30) {

output += getAlertHTML("30 દિવસ પહેલાની ગણતરી", "alert-info", d1, d30);

}




// 🔹 60 days check

let d60 = new Date(d2);

d60.setDate(d60.getDate() - 60);

if (d1 <= d60) {

output += getAlertHTML("60 દિવસ પહેલાની ગણતરી", "alert-warning", d1, d60);

}




// 🔹 90 days check

let d90 = new Date(d2);

d90.setDate(d90.getDate() - 90);

if (d1 <= d90) {

output += getAlertHTML("90 દિવસ પહેલાની ગણતરી", "alert-danger", d1, d90);

}




document.getElementById("reultdiv").innerHTML = output;

}

</script>




</body>

</html>

No comments:

Post a Comment