257

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

Community
  • 1
  • 1
user3574492
  • 6,225
  • 9
  • 52
  • 105
  • 2
    Possible duplicate of [Moment JS start and end of given month](http://stackoverflow.com/questions/26131003/moment-js-start-and-end-of-given-month) – Audite Marlow Sep 01 '16 at 09:23
  • 1
    Moment.js support the function to get the first and last day of month, you can get that for current month by : moment().startOf('month') and moment().endOf('month') . I think you should view the answer from [this](http://stackoverflow.com/questions/26131003/moment-js-start-and-end-of-given-month) – Tan Le Sep 01 '16 at 09:30
  • @TanLe Yes but this outputs in the following format: `Thu Sep 01 2016 00:00:00 GMT+0100 (GMT Summer Time)` I want the format as I've mentioned in my question – user3574492 Sep 01 '16 at 09:35
  • That function return the moment object for day, so you can use format to output with any format that you want. Like : moment().startOf('month').format('YYYY-MM-DD HH:mm'). – Tan Le Sep 01 '16 at 09:39
  • @AuditeMarlow Nice try but the questions are slightly different as explained in my edit. – user3574492 Sep 01 '16 at 09:46

9 Answers9

664

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');
Ali
  • 7,297
  • 2
  • 19
  • 19
  • 1
    Don't know why somebody deleted the original comments but the reason this was not the accepted answer was because it was posted many months after the accepted answer. – user3574492 May 17 '18 at 08:32
  • 2
    thanks @user3574492 not a big deal, thanks for clarification - if you think this answer is the best one then I think it is possible to change it to the accepted answer, even if it was posted later. – Ali May 18 '18 at 00:28
  • 4
    `moment(1, "DD");` will return first day of current month – Anil Vangari Mar 04 '19 at 17:02
  • 4
    why we need `clone` here ? – thelonglqd May 12 '21 at 02:43
  • 1
    .Clone() is needed since a lot of the functions will modify the moment date. One example is x = momentDate.add(1,"day"); momentDate will be modified here, adding one day as well as settting x. StartOf month will set the moment to the start of the month. – meh93 Sep 09 '21 at 20:34
  • we are not mutating the variables here so clone() is unnecessary. – Ali Sep 10 '21 at 01:13
48

There would be another way to do this:

var begin = moment().format("YYYY-MM-01");
var end = moment().format("YYYY-MM-") + moment().daysInMonth();
Akoya
  • 1,060
  • 12
  • 17
34

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);
Kevin Grosgojat
  • 1,386
  • 8
  • 13
20

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
ifelse.codes
  • 2,289
  • 23
  • 21
15

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')}
3

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')
Mo.
  • 26,306
  • 36
  • 159
  • 225
2

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');
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

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); 
Jojo Joseph
  • 1,493
  • 1
  • 15
  • 12
0
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

Fahad Khan
  • 39
  • 4