How to get the last day of a month with MomentJS?

2

I'm using MomentJS and I need to get the first and last day of the current month.

Currently, to get the first day of the month I'm doing this:

 var data_inicial = moment().format('01/MM/YYYY')

So far so good, since every month starts with 01 .

But what about the last day of the month? How can I get the last day of the month with MomentJs?

    
asked by anonymous 19.09.2018 / 15:03

2 answers

4

The solution is to use endOf('month') , like your Your own answer indicates.

There is only one detail: this method also changes the time. See the example below:

 console.log(moment([2018, 8, 19]).endOf('month'))
<script src="http://momentjs.com/downloads/moment.min.js"></script>

IntheexampleaboveIpassed[2018,8,19]asaparametertomoment,tocreatethedateSeptember19,2018(when this array , the months are indexed to zero: January is zero, February is 1, etc, so September is month 8 in that case). If you want the current date, just use moment() with no parameters.

In my case, the output was "2018-10-01T02:59:59.999Z" (yes 1 October, quite different from expected - but I already explained).

This happens because moment() uses the current date / time (September 19, 2018) and endOf('month') changes the day to the last of the month (30) and the time to 23: 59: 59.999, but always using the browser timezone .

In my case, the browser timezone is America/Sao_Paulo , that is, the resulting date is 2018-09-30T23:59:59.999-03:00 (September 30, 2018, at 23:59:59:59 in Sao Paulo - -03:00 offset indicates that we are 3 hours behind UTC). Converting to UTC results in 2018-10-01T02:59:59.999Z (the "Z" at the end indicates that it is in UTC ) .

When we use format() , the correct date is returned because browser timezone is used to get the date and time values:

 console.log(moment([2018, 8, 19]).endOf('month').format('DD/MM/YYYY HH:mm:ss.SSS'))
<script src="http://momentjs.com/downloads/moment.min.js"></script>

Nowtheoutputis30/09/201823:59:59.999.Thelastdayofthemonth(September30)withthetimechangedto23:59:59.999.

Sinceyou'reonlycatchingthedateonformatandignoringthetime,thisisnotaproblem.

Butifyouwanttokeepthesametime,justcreateanothermomentandsetthetime,using set and passing the values you want to change (in this case, the time fields):

// data e hora atual
var d1 = moment();

// obter o fim do mês e setar horário para o atual
var d2 = moment().endOf('month').set({
    'hour' : d1.get('hour'),
    'minute' : d1.get('minute'),
    'second' : d1.get('second'),
    'millisecond' : d1.get('millisecond')
});

console.log(d1.format('DD/MM/YYYY HH:mm:ss.SSS'));
console.log(d2.format('DD/MM/YYYY HH:mm:ss.SSS'));
<script src="http://momentjs.com/downloads/moment.min.js"></script>

Inthiscase,d2willhavethedateofthelastdayofthemonthandthetimewillbe"unchanged" (that is, it will be equal to the current time of the moment that moment was created).

Bonus

To find the browser timezone, use Momentjs Timezone and the guess() . It has this name because it is not always possible to know precisely the correct timezone (in documentation explains better).

And for the first day of the month you can also use startOf('month') . But it has a similar feature to endOf , which is to change the time to midnight.

    
19.09.2018 / 15:42
2

You can use the endOf method and add the 'month' parameter.

Example:

 moment().endOf('month').format('DD/MM/YYYY')
    
19.09.2018 / 15:03