I have 2 datepickers and a list of checkboxes of week days. A user can select a start date or end date and check any checkbox day. I want to count the number of
week days between 2 days.
For example: I want to join any yoga classes then I will select start or end date and also select week day like Monday , Tuesday .
Now i want to count the number of all Mondays and Tuesdays between 2 dates
date1 = Mar 01,2016
date2 = Apr 01,2016
I want to count number of day name between these date like this:
no of sunday: 4
no of monday: 4
no of tuesday: 5 etc..
I have tried this code
var d = new Date(date1);
var now = new Date(Date.now());
var daysOfYear = [];
count = 0;
for (d ; d <= date1 ; d.setDate(d.getDate() + 1)) {
val = $("#ch_"+d.getDay());
if(val.is(':checked')){
count++;
}
}
But it gives a TypeError: d.getDay is not a function
You need to iterate between dates and check the day
First convert the dates into a date object
date1 = convertToDateObj(date1); //assuming you already have a way to parse this string to date
date2 = convertToDateObj(date2); //assuming you already have a way to parse this string to date
Now iterate throught them
var dayCount = {0:0,1:0,2:0,3:0,4:0,5:0,6:0}; //0 is sunday and 6 is saturday
for (var d = date1; d <= date2; d.setDate(d.getDate() + 1))
{
dayCount[d.getDay()]++;
}
console.log(dayCount);
Related
I have to find week number of the month from given date using JavaScript. Week start is Monday.
I have tried the code below but not getting accurate result.
function getWeekNumber(date) {
var monthStartDate = new Date(new Date().getFullYear(), new Date().getMonth(), 1);
monthStartDate = new Date(monthStartDate);
var day = monthStartDate.getDay();
date = new Date(date);
var date = date.getDate();
let weekNumber = Math.ceil((date + (day)) / 7);
return (weekNumber == 0) ? 1 : weekNumber;
}
var week = getWeekNumber('2020-04-04');
console.log(week);
Try this one
function getWeek(date) {
let monthStart = new Date(date);
monthStart.setDate(0);
let offset = (monthStart.getDay() + 1) % 7 - 1; // -1 is for a week starting on Monday
return Math.ceil((date.getDate() + offset) / 7);
}
getWeek(new Date(2019, 2, 14))
You could find the week number of the month for weeks starting on Monday (in line with the ISO week date system) by rolling the input date back to the previous Monday and then dividing the Monday date by 7 and rounding up to determine which week of the month the date falls in.
This approach will properly handle dates at the beginning of a month which actually fall in the last week of the previous month. For instance, 2020-04-04 is a Saturday in the week starting on 2020-03-30 (Monday), so it should return week 5 since it is part of the 5th week of March (and not part of the 1st week of April which starts on 2020-04-06, the first Monday in April).
For example (the split bit at the beginning is just to parse the date string rather than relying on new Date() to parse the string since that is not recommended due to browser inconsistencies):
const monthWeek = (s) => {
const [y, m, d] = s.split('-'); // parse date string
const date = new Date(y, m - 1, d); // create date object
date.setDate(d - ((date.getDay() + 6) % 7)); // adjust date to previous Monday
return Math.ceil(date.getDate() / 7); // return week number of the month
};
console.log(monthWeek('2020-04-04'));
// 5
console.log(monthWeek('2020-04-07'));
// 1
For eg. Date is 2019-01-29 (Jan 29,2019)
I want set month January from 29 Date to 31 Date and display as 2019-01-31 as result using JavaScript
//Set this to whatever date you want..
var d = '2019-02-21';
//Parse out our date object a bit..
var asOf = new Date(d);
var year = asOf.getFullYear();
var month = asOf.getMonth();
//Initially set to first day of next month..
var desiredDate = new Date(year, month + 1, 1);
//Now just subtract 1 day to make it the last day of prior month..
desiredDate.setDate(desiredDate.getDate() - 1);
//Show the date of last day in month..
console.log(`The last day of ${month + 1}/${year} is: ${desiredDate.toLocaleString().split(',')[0]}`);
var date = new Date();
date.setMonth(date.getMonth() + 1);
date.setDate(-1);
Setting date to -1 sets it to the last day of the previous month.
I have some code, that is doing pretty much all i need it to do. Its calculating 3 days in the future, excluding dates, and then displaying my "estimated dispatch date"
The date, however displays in full date and time, instead of just date.
Day Month Date Year 12:02:57 GMT+0100 (British Summer Time)
Can anyone help with the code below, so that it excludes local time and only displays the future date, excluding weekend, DD/MM/YYYY or, in the below format;
Monday 20th June
Thanks in advance!
function addDates(startDate,noOfDaysToAdd){
var count = 0;
while(count < noOfDaysToAdd){
endDate = new Date(startDate.setDate(startDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
//Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
count++;
}
}
return startDate;
}
var today = new Date();
var daysToAdd = 3;
document.write ('Estimated Dispatch Date: ' + addDates(today,daysToAdd));
You can use the toDateString method to display just the date portion of your Date object, but you will need to use a few other methods for full control over the format of your date string...
You can display just the date, month and year parts of your local date and time with a few extra lines of code using the getDate, getMonth, and getFullYear methods to help with the formatting. You could try passing specific formatting parameters to toLocaleString, but this may display different results in different browsers. For example, the code below outputs a date in the format dd/mm/yyyy in Chrome but that output is not guaranteed across browsers.
new Date().toLocaleString('en-GB', {year: 'numeric', month: 'numeric', day: 'numeric'})
Not sure I am following how you want to handle weekend dates, so the below handles the date formatting that you want in the formatDate function separately from the addDays function where it just handles weekend dates by rolling the date forward to a Monday if the initially calculated date lands on a Saturday or Sunday.
// format input date to dd/mm/yyyy
const formatDate = (date) => {
const d = date.getDate(); // day of the month
const m = date.getMonth(); // month index from 0 (Jan) to 11 (Dec)
const yyyy = date.getFullYear(); // 4 digit year
const dd = (d < 10 ? '0' : '') + d; // format date to 2 digit
const mm = (m + 1 < 10 ? '0' : '') + (m + 1); // convert index to month and format 2 digit
return `${dd}/${mm}/${yyyy}`;
};
// add input days to today and adjust for weekend output
const addDays = (today, days) => {
const now = today.getTime() // now in UTC milliseconds
const ms = 24 * 60 * 60000; // milliseconds in one day
const date = new Date((days * ms) + now); // today plus input days
const day = date.getDay(); // weekday index from 0 (Sun) to 6 (Sat)
// adjust weekend results to next weekday
if (day === 0 || day === 6) {
let adj = day === 0 ? 1 : 2;
return new Date(((days + adj) * ms) + now);
}
return date;
};
document.write('Estimated Dispatch Date: ' + formatDate(addDays(new Date(), 3)));
I have a dropdown on my page named Quarter which has following values -
Quarter 1
Quarter 2
Quarter 3
Quarter 4
Also i have another field named Year with Year values - 2015,2016,2-17,2-18,2019,2020
I want to define the values for each quarter in my javascript code like
if(quarter = "quarter 1")
{
startdate = 2017-01-01;enddate = 2017-01-31
}
And so on for other quarters as well.
I would like to ask if there is built-in functionality to get the startdate and enddate for each quarter and i will append the year field value to it.so that it does not remain static.
Any help would be appreciated.
Thanks in Advance
Quarter of a financial year starts on April 1 for many countries, so I would argue that you need to set the start of quarter 1 first.
Let's say
var startDate = new Date();
date.setMonth(2);
date.setDate(1);
Then you can have functions like
function addQuarter(date)
{
date.setMonth( date.getMonth() + 3 );
return date;
}
For example
addQuarter(date); //returns Thu Jun 01 2017
Similarly to get last date of quarter
function lastDateOfQuarter( date )
{
date.setMonth( date.getMonth() + 3 );
date.setDate( date.getDate() - 1);
return date;
}
You can format the date as per your requirements for display
function formatDate( date )
{
return date.getDate() + "-" + ( date.getMonth() + 1 ) + "-" + date.getFullYear();
}
I would like to ask if there is built-in functionality to get the startdate and enddate for each quarter and i will append the year field value to it.
Those are constants. Given your example for Q1, you're talking about the normal calendar quarters of a year. Q1 starts on 01/01 and ends on 31/03, Q2 starts on 01/04 and ends on 30/06, etc. Since no quarter starts or ends on the last day of February, this is not a moving target.
No, there's no built-in functionality, but you don't need any. Just construct dates from those constant month and day values and the year you need.
var q1 = {
start: new Date(2017, 0, 1), // Remember, January = 0 here
end: new Date(2017, 2, 31)
};
// ...
Or for UTC:
var q1 = {
start: new Date(Date.UTC(2017, 0, 1)),
end: new Date(Date.UTC(2017, 2, 31))
};
// ...
//if you are using current year
var todayDate=new Date();
var startMonth=todayDate.getMonth();
var startDay=1;
var endMonth=todayDate.getMonth();
var endtDay=31;
switch(quarterValue){
case "Q1" : startMonth=0;endMonth=2;
case "Q2" : startMonth=3;endMonth=5;
case "Q3" : startMonth=6;endMonth=8;
case "Q4" : startMonth=9;endMonth=11;
}
todayDate.setMonth(startMonth);
todayDate.setDate(startDay);
var startDate=todayDate;
todayDate.setMonth(endMonth);
todayDate.setDate(endDay);
var endDate=todayDate;
I have a report create time as 2016-05-30, now I need to get the last 7 days from the report time.How can I get using moment?
report_create_time = moment('2016-05-30').format('MMM DD, YYYY');
I see this but it gives 7 days from the current date but I want from the report_Create_time.
dateFrom = moment().subtract(7,'d').format('YYYY-MM-DD');
you can try this pure javascript
var d = new Date('2016-05-30');
var day = d.getDate() - 7;
var month = d.getMonth();
var year = d.getFullYear();
var d1 = new Date(year+"-"+month+"-"+day);
alert(d1);
https://jsfiddle.net/c6c2vur8/
Small change needed
report_create_time = moment('2016-05-30');
dateFrom = report_create_time.subtract(7,'days');
report_create_time = report_create_time.format('MMM DD, YYYY'); // if you needed this formatted date to show in your HTML
dateFrom is the day before 7 days. so we need days from dateFrom to report_create_time
If you have both the dates, you can add 1 day from dateFrom up to seven days
var dates = []
for(var i=1; i<=7; i++){
dates[i-1] = dateFrom.add('1', 'days').fotmat('MMM DD, YYYY')
}
If you don't need this way, you can subtract 1 day from report_create_time 7 times