A) To determine the remaining days in a month, give this a try:
<script>
function getDaysInMonth(month,year) {
var today = new Date().getDate();
// Ensuring January is 1 based
// Day 0 represents the last day of the previous month
// var monthDays = new Date(year, month, 0).getDate();
// Making January 0 based instead
var monthDays = new Date(year, month+1, 0).getDate();
var remainDays = monthDays - today;
return remainDays;
}
console.log(getDaysInMonth(8, 2021));
</script>
B) For an interactive timer feature, use the following script:
<div class="counter" style='color: green;'>
<span class='e-m-days'>0</span> Days |
<span class='e-m-hours'>8</span> Hours |
<span class='e-m-minutes'>0</span> Minutes |
<span class='e-m-seconds'>1</span> Seconds
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function() {
function getCounterTimerData(obj) {
var days = parseInt($('.e-m-days', obj).text());
var hours = parseInt($('.e-m-hours', obj).text());
var minutes = parseInt($('.e-m-minutes', obj).text());
var seconds = parseInt($('.e-m-seconds', obj).text());
return seconds + (minutes * 60) + (hours * 3600) + (days * 3600 * 24);
}
function setCounterTimerData(s, obj) {
var days = Math.floor(s / (3600 * 24));
var hours = Math.floor((s % (60 * 60 * 24)) / (3600));
var minutes = Math.floor((s % (60 * 60)) / 60);
var seconds = Math.floor(s % 60);
console.log(days, hours, minutes, seconds);
$('.e-m-days', obj).html(days);
$('.e-m-hours', obj).html(hours);
$('.e-m-minutes', obj).html(minutes);
$('.e-m-seconds', obj).html(seconds);
}
var count = getCounterTimerData($(".counter"));
var timer = setInterval(function() {
count--;
if (count == 0) {
clearInterval(timer);
return;
}
setCounterTimerData(count, $(".counter"));
}, 1000);
});
</script>
You now have the ability to manually adjust day, minute, and hour settings.