Momentjs how to get next month last date - javascript

i am trying to get the last date of the next month, for some reason momentjs returns the wrong date
For Example :
console.log(moment('02/28/2018', "MM/DD/YYYY").add(i, 'M'));
returns :
moment("2018-03-28T00:00:00.000")
where the right date should be :
moment("2018-03-31T00:00:00.000")

Using .add() you are just adding a month to your actual date but you want also go to last day of this month, and for this you can use .endOf():
moment('02/28/2018', "MM/DD/YYYY").add(1, 'M').endOf("month")
// 2018-03-31T23:59:59.999Z

You are adding 1 month to 2/28, so you get 3/28. It only increases the month part.
To get the last day of the month, you can use:
// Should be 12/31/2018 23:59:59
console.log(moment("2018-12-01").endOf("month").toString());
// Should be last day, hour, minute, and second of the current month
console.log(moment().endOf("month").toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.21.0/moment.min.js"></script>

Another variation of guijob's answer, if you want to get based on current day you can use
moment().add(1, "month").endOf("month")

Related

Issue with date-picker in angular

There is some kind of "campaign duration" which means you are able to choose date between first and last day in month.
If you choose date which is not between first and last day in month, for example some date from next month, the Backend API will return an "Input param error".
if I choose the last date inside date selector(30th day in month), the Backend API will return an "Input param error". That means he count 30th day like 1st day in next month.
I should Change date selection with one day less, to be when I choose 30th day I should before send date by api, discount that number for one.
By the way date is in string format.
I need help how to discount string number for one before sending that date on api?
You can subtract one day from your date.
First you need to create a date from your string:
var dateFromString = '2011-04-11T10:20:30Z';
let newDate = new Date(dateFromString);
and then subtract one day from that date:
newDate.setDate(newDate.getDate() - 1);
You can use momentjs to convert string into date and subtract days as:
selectedEndDate = moment(selectedEndDate, 'DD/M/YYYY').subtract(1, 'day');

Angular date issue

I have an object with a date property. I set the date to 01 April 2000 and can see in the debugger that it is set properly to the same date. However when I do a getMonth() on the same date object it returns month as 3 ( March ) . Why is this happening. Does it have anything to do with UTC or Localization both of which i am not using ?
You need +1 for getMonth function
var month = date.getMonth() + 1
this is normal behavior. Months starts with Zero(i.e january is 0). thats why its giving 3 for april. use adding 1 with month to get exact month's digit value.
i guess, to help indexing javascript starts month with zero.
suppose an array of months in string form then we dont have to worry to fetch correct month from string.

Check if the input day is Monday with moment.js

Well, I want to check if a day of date is Monday like:
var myDate = new Date();
moment(myDate, 'DD-MM-YYYY').dayIs('monday')
In my country the first day of week is Monday, so, really I want check if the input date is begin of week.
I try using moment(myDate, 'DD-MM-YYYY').startOf('isoweek') but, this don't work for me.
According to the documentation - Moment.js has a locale setting which should solve this for you;
Day of Week (Locale Aware) 2.1.0+
moment().weekday(Number);
moment().weekday(); // Number
Gets or sets the day of the week according to the locale.
If the locale assigns Monday as the first day of the week, moment().weekday(0) will be Monday. If Sunday is the first day of the week, moment().weekday(0) will be Sunday.
Here you can see how to set the locale;
moment.locale('en'); // set to english

How to make bootstrap datepicker pick last day of the month instead of first day when minViewMode : 1

I'm using this version of Bootstrap DatePicker: http://eternicode.github.io/bootstrap-datepicker/ (the range type), and i have uploaded my demo on JSFiddle on here: http://jsfiddle.net/qs5co179/6/
The relevant JavaScript:
$('#sandbox-container .input-daterange').datepicker({
format: "dd/mm/yyyy",
minViewMode : 1
});
I have set the parameter minViewMode : 1 so I can view only months on calendar instead of full days, the reason is that I want to strict the user to enter:
1st day of the month in: input name="start"
and strict them too to enter only the last day of the month in: input name="end" (but the default is always 1st day of the month when you click)
For example my data entry that i am looking for like below:
From: 01/07/2015 - To: 30/09/2015.. in this format (dd/mm/yyyy)
Now I can't find any parameter to set the calender to pick the last day of the month instead of always 1st day when you click on the month in "To:" field.
one more thing also that I want to always make "From:" and "To:" having 3months period and no less, I want always to make the range 3 months minimum for the entery.
Thanks a lot in advance...
You could attach an event handler to the changeDate event (doc), and change the date manually (i.e. add a month, substract a day).
For the date handling part, I would
Get the date parts
Increase the month by one, if its December, then change it to January
Create a new Date instance
date = date.setDate(date.getDate() - 1) to get the correct day
The date parts could be exctracted by simple string slicing:
var day = str.slice(0,2);
var month = str.slice(3,5);
var year = str.slice(6,10);

Getting the next date for a specific day of the week

Given a start date of 2014-07-08 (Tuesday) I would want to perform a check to find the closest day of the week.
For example, I need to be able to perform the following calls:
First Monday (should return 2014-07-14)
First Wednesday (should return 2014-07-09)
First Saturday (should return 2014-07-12)
Etc.
I know moment.js lets you do something like
moment("2014-07-08").day(1)
To get the date of Monday this week, however I need to know if the DOW is before/after the current date and apply the offset accordingly; if that makes any sense..
Any thoughts?
Just add 7 if the day of the week you are looking for is or was before the current day of the week:
var date = moment("2014-07-08");
var dow = date.day();
var nextX = date.day(X + dow <= X ? 7 : 0);

Categories

Resources