最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get the number of days in a month - Stack Overflow

programmeradmin3浏览0评论

I make a countdown timer, I need to take the number of days in a month and subtract the days that have passed (for example: 30 days of September minus 8 days - the date is today). Now I have to enter it manually:

const end = new Date(2021, 8, 30, 13, 0,12, 12);

I make a countdown timer, I need to take the number of days in a month and subtract the days that have passed (for example: 30 days of September minus 8 days - the date is today). Now I have to enter it manually:

const end = new Date(2021, 8, 30, 13, 0,12, 12);
Share Improve this question asked Sep 8, 2021 at 9:05 AlexAlex 3754 silver badges19 bronze badges 3
  • You can gete the number of days in a month using: new Date(year, month, 0).getDate(); – Karma Blackshaw Commented Sep 8, 2021 at 9:08
  • You want to know how many days are left in the current month? – maki000 Commented Sep 8, 2021 at 9:08
  • Wait... 30 days - 8 days - date today? – Karma Blackshaw Commented Sep 8, 2021 at 9:10
Add a ment  | 

4 Answers 4

Reset to default 5

You can get the number of days in a month using:

const getDays = (year, month) => new Date(year, month, 0).getDate()

const days = getDays(2021, 8)
console.log(days)

Days left in current month

var currentDate = new Date();
var currentYear = currentDate.getFullYear();
var currentMonth = currentDate.getMonth();

var currentMonthLastDate = (new Date(currentYear, currentMonth, 0)).getDate();

var daysLeftInMonth = currentMonthLastDate - currentDate.getDate();

console.log(daysLeftInMonth);

A) If you won't only get the number of days in a month try this one:

<script>
    function getDaysInMonth(month,year) {

       var today = new Date().getDate();

       // Here January is 1 based
       // Day 0 is the last day in the previous month

       // var monthDays = new Date(year, month, 0).getDate();

       // Here January is 0 based
       var monthDays =  new Date(year, month+1, 0).getDate();

       var remainDays = monthDays - today;

       return remainDays;
   }

   console.log(getDaysInMonth(8, 2021));

</script>

B) If you want to get a full-timer to follow this one:

<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./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>

Now you can set day, minutes, hours manually

 var dt = new Date();
 var month = dt.getMonth(); 
 returns month
 var year = dt.getFullYear();
 var daysInMonth = new Date(year, month, 0).getDate(); 
// returns the diff

发布评论

评论列表(0)

  1. 暂无评论