Need to Get the Dates of All Mondays in a Year

Get all mondays in the year

Just loop through the year instead of the month. The code is the same as yours, it works fine. just changed month -> year and getMonth() -> getYear()

var d = new Date(),
year = d.getYear(),
mondays = [];

d.setDate(1);

// Get the first Monday in the month
while (d.getDay() !== 1) {
d.setDate(d.getDate() + 1);
}

// Get all the other Mondays in the month
while (d.getYear() === year) {
var pushDate = new Date(d.getTime());
mondays.push(pushDate.getDate() + '-' + (pushDate.getMonth()+1) + '-' + pushDate.getFullYear());
d.setDate(d.getDate() + 7);
}

Need to get the dates of all mondays in a year

To get all mondays, by getting all dates and filtering by Mondays:

for i in `seq 0 365`
do date -d "+$i day"
done | grep Mon

Of course, you could also take a monday and keep incrementing by 7 days.

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.

Trying to list all the mondays and their dates in a year

A DateTime instance is immutable, meaning that it cannot be changed. The d2.AddDays(7) expression creates a new instance with a different value, which you can assign back to d2:

d2 = d2.AddDays(7)

So why does your program crash? Since the date never changes, it will never hit 2015, and your program will try to add infinitely many items to the listbox. Obviously you will run out of memory before that happens, which makes the program crash.

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 get the Mondays (full date) in the current month?

Try the next code, please:

Sub NumMondays()
Dim i As Long, month_name, year_name, num_mondays As Integer
Dim test_date As Date, orig_month As Integer, arrMondays, k As Long

month_name = Format(Date, "mmmm")
year_name = Format(Date, "yyyy")
' Get the first day of the month.
test_date = CDate(month_name & " 1, " & year_name)

orig_month = month(test_date)
ReDim arrMondays(4)
'extract and count Mondays:
Do
If Weekday(test_date, vbMonday) = 1 Then
arrMondays(k) = test_date: k = k + 1: num_mondays = num_mondays + 1
End If
test_date = test_date + 1
Loop While (month(test_date) = orig_month)
ReDim Preserve arrMondays(k - 1)

Debug.Print "Current month no = " & orig_month
Debug.Print "No of Mondays = " & num_mondays
Debug.Print Join(arrMondays, ", ")
End Sub

How can I select all of the Sundays for a year using Python?

You can use date from the datetime module to find the first Sunday in a year and then keep adding seven days, generating new Sundays:

from datetime import date, timedelta

def allsundays(year):
d = date(year, 1, 1) # January 1st
d += timedelta(days = 6 - d.weekday()) # First Sunday
while d.year == year:
yield d
d += timedelta(days = 7)

for d in allsundays(2010):
print(d)


Related Topics



Leave a reply



Submit