JavaScript Moment JS last day of month issue moment last month first day,moment last month,moment last

I am using momentJS in a project and I have a function that takes a month and year and returns the last day of the month using those parameters.

Everything is working fine, Jan - November, and as soon as I use December, it is returning January.

Any ideas how I can tweak this to work? I am passing the true month value (5 = May) and then subtracting a month within the function to make it 0 based for moment to function correctly.

Fiddle: https://jsfiddle.net/bhhcp4cb/

// Given a year and month, return the last day of that month
function getMonthDateRange(year, month) {

    // 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]).add(-1,"month");

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

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

// Should be December 2016
console.log(moment(getMonthDateRange(2016, 12).end).toDate())

// Works fine with November
console.log(moment(getMonthDateRange(2016, 11).end).toDate())
Answer:1

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.

Answer:2

In this JSFiddle I have implemented elements in svg. I want the group of elements to be draggable and I have tried it with d3.drag and using transform:translate. The drag is not smooth. It flickers ...

In this JSFiddle I have implemented elements in svg. I want the group of elements to be draggable and I have tried it with d3.drag and using transform:translate. The drag is not smooth. It flickers ...

I'm just starting to document some Javascript code that I've written and I have a question. Looking through my vendor directory to see how the 3rd party packages do it, I've noticed that many of them ...

I'm just starting to document some Javascript code that I've written and I have a question. Looking through my vendor directory to see how the 3rd party packages do it, I've noticed that many of them ...

Functionality: A user has to enter a lecture code into an input field to enter a lecture. On the landing page, while the user types the lecture code, I want check if the code is correct and colour the ...

Functionality: A user has to enter a lecture code into an input field to enter a lecture. On the landing page, while the user types the lecture code, I want check if the code is correct and colour the ...

  1. meteor call template helper
  2. meteor call template helper from event
  3. meteor call function from template

I am trying to alert the value one of textbox of form in JavaScript but its not working and my other forms have same code of JavaScript they are working fine. I have spent lot of time to fix this ...

I am trying to alert the value one of textbox of form in JavaScript but its not working and my other forms have same code of JavaScript they are working fine. I have spent lot of time to fix this ...

  1. alert value textbox javascript
  2. alert textbox value in jquery
  3. alert textbox value
  4. alert on textbox value change jquery
  5. js alert textbox value
  6. get value textbox javascript alert