Moment js get first and last day of current month

JavascriptDateMomentjs

Javascript Problem Overview


How do I get the first and last day and time of the current month in the following format in moment.js:

> 2016-09-01 00:00

I can get the current date and time like this: moment().format('YYYY-MM-DD h:m') which will output in the above format.

However I need to get the date and time of the first and last day of the current month, any way to do this?

EDIT: My question is different from this one because that asks for a given month that the user already has whereas I am asking for the current month along with asking for a specific format of the date that is not mentioned in the other so called 'duplicate'.

Javascript Solutions


Solution 1 - Javascript

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');

Solution 2 - Javascript

There would be another way to do this:

var begin = moment().format("YYYY-MM-01");
var end = moment().format("YYYY-MM-") + moment().daysInMonth();

Solution 3 - Javascript

You can do this without moment.js

A way to do this in native Javascript code :

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);

firstDay = moment(firstDay).format(yourFormat);
lastDay = moment(lastDay).format(yourFormat);

Solution 4 - Javascript

moment startOf() and endOf() is the answer you are searching for.. For Example:-

moment().startOf('year');    // set to January 1st, 12:00 am this year
moment().startOf('month');   // set to the first of this month, 12:00 am
moment().startOf('week');    // set to the first day of this week, 12:00 am
moment().startOf('day');     // set to 12:00 am today

Solution 5 - Javascript

Assuming you are using a Date range Picker to retrieve the dates. You could do something like to to get what you want.

$('#daterange-btn').daterangepicker({
            ranges: {
                'Today': [moment(), moment()],
                'Yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')],
                'Last 7 Days': [moment().subtract(6, 'days'), moment()],
                'Last 30 Days': [moment().subtract(29, 'days'), moment()],
                'This Month': [moment().startOf('month'), moment().endOf('month')],
                'Last Month': [moment().subtract(1, 'month').startOf('month'), moment().subtract(1, 'month').endOf('month')]
            },
            startDate: moment().subtract(29, 'days'),
            endDate: moment()
        }, function (start, end) {
      alert( 'Date is between' + start.format('YYYY-MM-DD h:m') + 'and' + end.format('YYYY-MM-DD h:m')}

Solution 6 - Javascript

As simple we can use daysInMonth() and endOf()

const firstDay = moment('2016-09-15 00:00', 'YYYY-MM-DD h:m').startOf('month').format('D')
const lastDay = moment('2016-09-15 00:00', 'YYYY-MM-DD h:m').endOf('month').format('D')

Solution 7 - Javascript

I ran into some issues because I wasn't aware that moment().endOf() mutates the input date, so I used this work around.

let thisMoment = moment();
let endOfMonth = moment(thisMoment).endOf('month');
let startOfMonth = moment(thisMoment).startOf('month');

Solution 8 - Javascript

First and Last Date of current Month In the moment.js

console.log("current month first date");
    const firstdate = moment().startOf('month').format('DD-MM-YYYY');
console.log(firstdate);
   
console.log("current month last date");
    const lastdate=moment().endOf('month').format("DD-MM-YYYY"); 
console.log(lastdate); 
    

Solution 9 - Javascript

console.log("current month first date");
    const firstdate = moment().startOf('month').format('DD-MM-YYYY');
console.log(firstdate);

console.log("current month last date");
    const lastdate=moment().endOf('month').format("DD-MM-YYYY"); 
console.log(lastdate);

// THIS IS THE SIMPLEST OF GETTING DATES IN MOMENTJS

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3574492View Question on Stackoverflow
Solution 1 - JavascriptAliView Answer on Stackoverflow
Solution 2 - JavascriptAkoyaView Answer on Stackoverflow
Solution 3 - JavascriptKevin GrosgojatView Answer on Stackoverflow
Solution 4 - Javascriptifelse.codesView Answer on Stackoverflow
Solution 5 - JavascriptAlexander Nana OwusuView Answer on Stackoverflow
Solution 6 - JavascriptMo.View Answer on Stackoverflow
Solution 7 - JavascriptTinashe DellanView Answer on Stackoverflow
Solution 8 - JavascriptJojo JosephView Answer on Stackoverflow
Solution 9 - JavascriptFahad KhanView Answer on Stackoverflow