How to Find Number of Mondays or Tuesdays Between Two Dates

how to find number of mondays or tuesdays between two dates?

You could create a function that uses strtotime() recursively to count the number of days. Since strtotime("next monday"); works just fine.

function daycount($day, $startdate, $counter)
{
if($startdate >= time())
{
return $counter;
}
else
{
return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
}
}

echo daycount("monday", strtotime("01.01.2009"), 0);

Hopefully this is something you're looking for :)

How to get all sundays/mondays/tuesdays between two dates?

Use a loop to continually get the date for next Sunday until you pass the ending date.

var start = moment('2016-09-01'), // Sept. 1st    end   = moment('2016-11-02'), // Nov. 2nd    day   = 0;                    // Sunday
var result = [];var current = start.clone();
while (current.day(7 + day).isBefore(end)) { result.push(current.clone());}
console.log(result.map(m => m.format('LLLL')));
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.17.1/moment.min.js"></script>

How to find number of Mondays or any other weekday between two dates in Python?

This code still uses a for loop and an if/else.

import datetime
import calendar

def weekday_count(start, end):
start_date = datetime.datetime.strptime(start, '%d/%m/%Y')
end_date = datetime.datetime.strptime(end, '%d/%m/%Y')
week = {}
for i in range((end_date - start_date).days):
day = calendar.day_name[(start_date + datetime.timedelta(days=i+1)).weekday()]
week[day] = week[day] + 1 if day in week else 1
return week

print(weekday_count("01/01/2017", "31/01/2017"))

# prints result
# {'Monday': 5, 'Tuesday': 5, 'Friday': 4, 'Wednesday': 4, 'Thursday': 4, 'Sunday': 5, 'Saturday': 4}

How to get the dates for all mondays between two dates but only for whole weeks within the range with PHP?

One possible approach:

function isMonday($date) {
return $date->format('N') === '1';
}

function isSunday($date) {
return $date->format('N') === '7';
}

function getMondays($start, $end) {
$mondays = [];

$datePeriod = new DatePeriod($start, new DateInterval('P1D'), $end);
foreach ($datePeriod as $date) {
if (isMonday($date)) $mondays[] = $date;
}
if (!isSunday($end)) array_pop($mondays);
return $mondays;
}

$startDate = new DateTime('2021-06-20');
$endDate = new DateTime('2021-07-07');
var_dump(getMondays($startDate, $endDate));

3v4l Demo. It's rather direct: there's a DatePeriod Traversable object created from $start and $end dates with 1 day interval, that is iterated with foreach; each date that's a Monday is stored in an array. Last step is discarding the very last item of that array if the endDate is not Sunday.

Get number of weekdays (Sundays, Mondays, Tuesdays) between two dates SQL

Given what I think you're trying to get, this should do it:

SET DATEFIRST 1

DECLARE
@start_date DATETIME,
@end_date DATETIME

SET @start_date = '2011-07-11'
SET @end_date = '2011-07-22'

;WITH Days_Of_The_Week AS (
SELECT 1 AS day_number, 'Monday' AS day_name UNION ALL
SELECT 2 AS day_number, 'Tuesday' AS day_name UNION ALL
SELECT 3 AS day_number, 'Wednesday' AS day_name UNION ALL
SELECT 4 AS day_number, 'Thursday' AS day_name UNION ALL
SELECT 5 AS day_number, 'Friday' AS day_name UNION ALL
SELECT 6 AS day_number, 'Saturday' AS day_name UNION ALL
SELECT 7 AS day_number, 'Sunday' AS day_name
)
SELECT
day_name,
1 + DATEDIFF(wk, @start_date, @end_date) -
CASE WHEN DATEPART(weekday, @start_date) > day_number THEN 1 ELSE 0 END -
CASE WHEN DATEPART(weekday, @end_date) < day_number THEN 1 ELSE 0 END
FROM
Days_Of_The_Week

Count number of Mondays in a given date range

Try this:

static int CountDays(DayOfWeek day, DateTime start, DateTime end)
{
TimeSpan ts = end - start; // Total duration
int count = (int)Math.Floor(ts.TotalDays / 7); // Number of whole weeks
int remainder = (int)(ts.TotalDays % 7); // Number of remaining days
int sinceLastDay = (int)(end.DayOfWeek - day); // Number of days since last [day]
if (sinceLastDay < 0) sinceLastDay += 7; // Adjust for negative days since last [day]

// If the days in excess of an even week are greater than or equal to the number days since the last [day], then count this one, too.
if (remainder >= sinceLastDay) count++;

return count;
}


Related Topics



Leave a reply



Submit