Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

269
Views
How do I get the days between the today's day and the last day of the month using Moment.js?

Here's the code that I have right now:

  const moment = require('moment')
    const m = moment


    const currDay = m().format('D')
    const dayOfWeek = m().format('dddd')
    const daysInMonth = m().daysInMonth()

    const startOfMonth = moment().startOf('month').format('YYYY-MM-DD hh:mm');
    const endOfMonth   = moment().endOf('month').format('YYYY-MM-DD hh:mm');

I need to create a calendar row where the first item would be the todays date, and the rest of the calendar items would be the whatever amount of days are left depending on the current month so I could render each day in between in my HTML with Vue.

Example: Wed 8, Thu 9, Fri 10 ... Fri 31.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I think the OP is tripped up on the common mistake of formatting prematurely. format is good to see an intermediate result, but doing so produces a string that's no good for additional calculation.

Try to handle date objects only. Convert to strings only when you must: (a) presenting to a human reader, or (b) serializing for storage or transmission.

Working without formatting...

const daysRemainingThisMonth = moment().endOf('month').diff(moment(), 'days');
console.log(`There are ${daysRemainingThisMonth} days remaining this month`)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

over 4 years ago · Santiago Trujillo Report

0

Just as a POJS equivalent, if you have a function to return the last day of the month, you can use that and just get the difference between the two dates, e.g.

function getMonthEnd(date = new Date()) {
  return new Date(date.getFullYear(), date.getMonth() + 1, 0);
}

function getMonthDaysLeft(date = new Date()) {
  return getMonthEnd(date).getDate() - date.getDate();
}

let d = new Date();
console.log(`There are ${getMonthDaysLeft(d)} days left in ${d.toLocaleString('en',{month:'long'})}.`);

To get a list/array of the days remaining, just loop over a date, adding 1 day at a time, and write the dates in the required format into the list:

function getMonthDaysLeftAsList(date = new Date()) {
  let d = new Date(+date);
  // Formatter
  let f = new Intl.DateTimeFormat('en',{
    day: 'numeric',
    month: 'short'
  });
  let m = d.getMonth();
  let dayList = [];
  while (d.getMonth() == m) {
    dayList.push(f.format(d));
    d.setDate(d.getDate() + 1);
  }
  return dayList;
}

console.log(getMonthDaysLeftAsList());

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!