Find the Date for the First Monday After a Given Date

Find the date for the first Monday after a given date

import datetime
def next_weekday(d, weekday):
days_ahead = weekday - d.weekday()
if days_ahead <= 0: # Target day already happened this week
days_ahead += 7
return d + datetime.timedelta(days_ahead)

d = datetime.date(2011, 7, 2)
next_monday = next_weekday(d, 0) # 0 = Monday, 1=Tuesday, 2=Wednesday...
print(next_monday)

Get first Monday after certain date?

Do like this:

GregorianCalendar date = new GregorianCalendar( year, month, day ); 

while( date.get( Calendar.DAY_OF_WEEK ) != Calendar.MONDAY )
date.add( Calendar.DATE, 1 );

You can now extract the year, day and month from date. Remember that month is 0 based (e.g. January = 0, Febuary = 1, etc.) and day is not.

php first monday after certain date as string

you can use this

<?php
$myDate = "2014-08-27";
$next_monday = date('Y-m-d', strtotime("next monday", strtotime($myDate)));
echo $next_monday;
?>

OUTPUT :

2014-09-01

Demo

How to calculate the first Monday of the month; python 3.3+

One way to do this is to ignore the passed in day value, and just use 7 instead; then you can simply subtract the weekday offset:

def find_first_monday(year, month, day):
d = datetime(year, int(month), 7)
offset = -d.weekday() #weekday = 0 means monday
return d + timedelta(offset)

Get first next Monday after certain date?

Java 8+

LocalDate ld = LocalDate.of(2014, Month.JUNE, 12);
System.out.println(ld);
ld = ld.with(TemporalAdjusters.next(DayOfWeek.MONDAY));
System.out.println(ld);

Which prints...

2014-06-12
2014-06-16

Because it's possible that the date my actually be a Monday, you could also use...

ld = ld.with(TemporalAdjusters.nextOrSame(DayOfWeek.MONDAY));

Java <= 7

You should be using the ThreeTen Backport, which gives you the support of the Java 8 Date/Time API

Original Answer

Instead of System.out.println(date1); use System.out.println(date1.getTime());

getTime returns an instance of Date which represents the current state of the Calendar

Which will output Mon Jul 14 00:00:00 EST 2014

System.out.println(date1) is the equivlent of using System.out.println(date1.toString()), which, in this case, is dumping a bunch of useful info about the state of the Calendar object, but not really human readable data.

System.out.println(date1.getTime()) will use the Date's to toString method to display a date value, formatted based on the current local settings, which will provide more useful information.

Updated

Instead of using GregorianCalendar, you should use the system Calendar, for example...

Calendar date1 = Calendar.getInstance();
date1.set(2014, 06, 12);

Also, months are 0 indexed, meaning that Janurary is actually 0 not 1, so in your example, you've specified the month as July, not June.

So, instead, using...

Calendar date1 = Calendar.getInstance();
date1.set(2014, 05, 12);

while (date1.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
date1.add(Calendar.DATE, 1);
}

System.out.println(date1.getTime());

Which outputted...

Mon Jun 16 16:22:26 EST 2014

Which is next Monday from today...more or less ;)

In Python, how do I find the date of the first Monday of a given week?

>>> import time
>>> time.asctime(time.strptime('2008 50 1', '%Y %W %w'))
'Mon Dec 15 00:00:00 2008'

Assuming the first day of your week is Monday, use %U instead of %W if the first day of your week is Sunday. See the documentation for strptime for details.

Update: Fixed week number. The %W directive is 0-based so week 51 would be entered as 50, not 51.

VBA - Trying To Find First Monday Of Given Month

You were close. For your test dates, this gave me the expected response:

Public Function FirstMonday(myDate As Date) As Date
Dim d As Date, w As Long
d = DateSerial(Year(myDate), Month(myDate), 1)
w = Weekday(d, vbMonday)

If w > 3 Then
FirstMonday = d + (8 - w)
Else
FirstMonday = d - w + 1
End If

End Function

When w > 3, the first day of a month is Thursday to Sunday. In that case, add the days missing to the following Monday (8 - w). Else, go backwards and substract w - 1 to get to the previous week's Monday.

Find if a given date is the first monday of the month

Give this a try:

$first_day_of_week = date('l', strtotime('9/2/2013'));
$date = intval(date('j', strtotime('9/2/2013')));
if ($date <= 7 && $first_day_of_week == 'Monday') {
// It's the first Monday of the month.
}

It seems kind of asinine, I know, but it allows you to replace '9/2/2013' with a variable if needed.

How to get the date for the start of the week (Monday) by passing in the current day?

Try this formula for a date in A2,

=A2-WEEKDAY(A2, 2)+1


Related Topics



Leave a reply



Submit