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');
Related
how do I save the day of the week to array from a given date range. For example, from 8/23/2021 to 12/11/2021, I would like to get the dates that are Mondays and save these dates to an array? Thanks
So you can use the getDay method.
Given your date:
var someday = new Date('8/23/2021');
using the getDay against the someday will return the day of the week index. 1 in this case.
Edit (Side-note): Looks like Sunday is the start of the week for JS
We can pass some options to get the proper long day and formatting.
var long_day = new Intl.DateTimeFormat('en-US', { weekday: 'long'}).format(someday);
This will return, Monday. You could save it when its 1/Monday, up to you.
JSFiddle
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")
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
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);
I am building a project and in that project the user is going to input a date eg. 8/2/2011. then i am going to show them the information for the week that contains 8/2/2011. How can you figure out which week to show? for this project i am using javascript, jquery, and php.
You should be able to do what you want by getting the first day of the week the given date occurs in.
If your weeks start on Monday:
Date.prototype.lastMonday=function(){
var d= new Date(this), weekstart= 1;
while(d.getDay()!== 1) d.setDate(d.getDate()-1)
return d;
}
alert(new Date().lastMonday())
//returns the current date if does fall on a Monday
If you want to know the days that compose the week you can just find out the day of the week and show the appropriate days before and after. To know the day of the week just use the getDay method of the Date object(see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/getDay). For more info on the Date object see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date.