Moment Js Get First and Last Day of Current Month

Moment js get first and last day of current month

In case anyone missed the comments on the original question, you can use built-in methods (works as of Moment 1.7):

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

Moment JS start and end of given month

That's because endOf mutates the original value.

Relevant quote:

Mutates the original moment by setting it to the end of a unit of time.

Here's an example function that gives you the output you want:

function getMonthDateRange(year, month) {
var moment = require('moment');

// month in moment is 0 based, so 9 is actually october, subtract 1 to compensate
// array is 'year', 'month', 'day', etc
var startDate = moment([year, month - 1]);

// Clone the value before .endOf()
var endDate = moment(startDate).endOf('month');

// just for demonstration:
console.log(startDate.toDate());
console.log(endDate.toDate());

// make sure to call toDate() for plain JavaScript date type
return { start: startDate, end: endDate };
}

References:

  • endOf()
  • clone()
  • Date from object

moment.js: Return the first and last day of each month within an interval

In to subarray that you push in main array you forgot to calculate the date for the month end.
It's better to use start and end Of in this case, without became crazy to calculate the correct number of day of month

const startDate = moment('2021-08-23');
const endDate = moment('2022-03-22');

const months = [];

if (endDate.isBefore(startDate)) {
throw Error('End date must be greater than start date.');
}

while (startDate.isBefore(endDate)) {
months.push([
startDate.startOf('month').format('YYYY-MM-DD'),
startDate.endOf('month').format('YYYY-MM-DD')
]);
startDate.add(1, 'month');
}

console.log(months);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

MomentJS - How to get last day of previous month from date?

Simply add a endOf('month') to your calls:

var dateFrom = moment(dateFrom).subtract(1,'months').endOf('month').format('YYYY-MM-DD');

http://jsfiddle.net/r42jg/327/

How to get last month day 15 and current month day 16 in moment

To get last month with date 15 you need this:

moment().subtract(1, 'month').date(15);

You subtract one month and set date to 15. This returns 15 october.

To get current date 15, just remove the subtract part.

To get exactly the result you asked for then:

const currentMonthDate15 = moment().date(15);
const lastMonthDate15 = moment().date(15).subtract(1, 'month');
const string = lastMonthDate15.format('DD MMM') + ', ' + currentMonthDate15.format('DD MMM YYYY');

Where string is 15 Oct, 15 Nov 2020

How to get first and last day of the week with Moment.js?

You are on the right track. The weekday function can be used to get the Monday and Friday of the current week.

var now = moment();
var monday = now.clone().weekday(1);
var friday = now.clone().weekday(5);
var isNowWeekday = now.isBetween(monday, friday, null, '[]');

console.log(`now: ${now}`);
console.log(`monday: ${monday}`);
console.log(`friday: ${friday}`);
console.log(`is now between monday and friday: ${isNowWeekday}`);

Direct link to the Moment.js docs on the weekday function here: https://momentjs.com/docs/#/get-set/weekday/

MomentJS: how to get last month date (not start of the month)?

You need to add 1 day onto your result if you're wanting the day after the day exactly 1 month ago:

moment().subtract(1, 'month').add(1, 'day');

console.log("Today:", moment().format("Do MMMM"));console.log("Month ago:", moment().subtract(1, 'month').add(1, 'day').format("Do MMMM"));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>

Moment.js returns 1 as last day of month

To get the day of the month, you have to use .date() instead. See the relevant part of the docs: http://momentjs.com/docs/#/get-set/date/

Moment JS last day of month issue

Instead of:

var startDate = moment([year, month]).add(-1,"month");

Do this:

var startDate = moment([year, month-1]);

Basically, you don't want to start at the wrong point and then move by a month, you simply want to start at the correct point.



Related Topics



Leave a reply



Submit