简体   繁体   中英

Get next date from weekday in Google Sheets/Excel

How can one return the next date of a given weekday (it could be either a number 1-7 or names Sunday-Saturday).

For example, if today, on Friday 16-Oct-2009 I passed in:

Friday, it would return today's date 16-Oct-2009
Saturday returns 17-Oct-2009
Thursday returns 22-Oct-2009

Here is the Spreadsheet

I have tried this formula:

=A12+(C12+ MOD(7-day(A12),7))

在此处输入图像描述

I worked out an answer to my own question

Here is the formula

=date(year(A12),month(A12), day(A12) + mod(C12+ 7-WEEKDAY(A12),7))

This worked for me:

=TODAY()+MOD(4 + 7-WEEKDAY(TODAY()),7)

Just replace TODAY() and 4 (weekday#) for your context.

This is the formula you are seeking.

=A12+(7-WEEKDAY(A12)+B12)

A12 holds the start date. B12 holds the ID of the targeted weekday, eg 1 for "Sunday".

A11 must hold a true date, as opposed to a text string that looks like a date. If the latter is the case an error will be displayed. The result is always a date. You can format that date as "dddd" to display a day's name or "dddd, dd-mm-yyyy" to display both date and day.

And here are the mechanics of the formula:- A12-WEEKDAY(A12) returns always the previous Saturday, a date that is always in the past. A12+(7-WEEKDAY(A12)) inverts that calculation, returning always the next Saturday. This date is always in the future except when A12 is a Saturday. That's when the result will be = A12.

Having found a stable base, you can just add the day you want to the Saturday you have. Add 1 (for Sunday) and you get a Sunday. Add 7 and you get a Saturday. All these dates will be in the future.

That's all as you want it except that if WEEKDAY(A12) = B12 you want the same date as in A12 which is a week earlier than the one the formula prefers. This is where I went wrong while in haste. My apologies! This is the correct modification of the formula's result, to wit, conditionally deduct 7 from the calculation.

=A12+(7-WEEKDAY(A12)+B12)-IF(WEEKDAY(A12)=B12,7,0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM