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
Related
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');
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
Moment.js is awesome, it lets you format dates to whatever format you need. But is there any function that allows you to list all the weekend dates (in DOY format) for the particular year?
it can be done easily using a JS loop and adding dates functionality of moment.js. Take an year set date to 1th of Jan., check the weekday and then go to next date by momentjs function add(1, 'days')
hope it helps
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.
I have the following code
datePicker.change(function(){
dateSet = datePicker.val();
dateMinimum = dateChange();
dateSetD = new Date(dateSet);
dateMinimumD = new Date(dateMinimum);
if(dateSetD<dateMinimumD){
datePicker.val(dateMinimum);
alert('You can not amend down due dates');
}
})
dateSet = "01/07/2010"
dateMinimum = "23/7/2010"
Both are UK format. When the date objects are compared dateSetD should be less than dateMinimumD but it is not. I think it is to do with the facts I am using UK dates dd/mm/yyyy. What would I need to change to get this working?
The JavaScript Date constructor doesn't parse strings in that form (whether in UK or U.S. format). See the spec for details, but you can construct the dates part by part:
new Date(year, month, day);
MomentJS might be useful for dealing with dates flexibly. (This answer previously linked to this lib, but it's not been maintained in a long time.)
This is how I ended up doing it:
var lastRunDateString ='05/04/2012'; \\5th april 2012
var lastRunDate = new Date(lastRunDateString.split('/')[2], lastRunDateString.split('/')[1] - 1, lastRunDateString.split('/')[0]);
Note the month indexing is from 0-11.
var dateString ='23/06/2015';
var splitDate = dateString.split('/');
var month = splitDate[1] - 1; //Javascript months are 0-11
var date = new Date(splitDate[2], month, splitDate[0]);
Split the date into day, month, year parts using dateSet.split('/')
Pass these parts in the right order to the Date constructor.
Yes, there is problem with the date format you are using. If you are not setting a date format the default date that is used is 'mm/dd/yy. So you should set your preferred date formate when you create it as following when you create the date picker:
$(".selector" ).datepicker({ dateFormat: 'dd/mm/yyyy' });
or you can set it later as:
$.datepicker.formatDate('dd/mm/yyyy');
When you try to create a date object:
new Date(year, month, day, hours, minutes, seconds, milliseconds)
Example:
dateSetD = new Date(dateSet.year, dateSet.month, dateSet.day);
Note: JavaScript Date object's month starts with 00, so you need to adjust your dateset accordingly.