How to get last Sunday's date?
The formula you're looking for would be:
=DATE(YY, MM, DD) - (WEEKDAY(DATE(YY, MM, DD)) - 1) - 7
// OR
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
Depending on what you take to be "last Sunday," you can simplify/factor this:
If you take "last Sunday" to mean, "the Sunday which has most recently happened:"
=DATE(A2,B2,C2) - WEEKDAY(DATE(A2,B2,C2)) + 1
If you take "last Sunday" to mean, "the Sunday which took place during the previous week:"
=DATE(A4,B4,C4) - WEEKDAY(DATE(A4,B4,C4)) - 6
Working through it:
=TODAY() - (WEEKDAY(TODAY()) - 1) - 7
TODAY()
// get today's date, ie. 22/11/2019
WEEKDAY(TODAY())
// get the current day of the week, ie. 6 (friday)
-
// take the first date and subtract that to rewind through the week,
// ie. 16/11/2019 (last saturday, since saturday is 0)
- 1
// rewind back one less than the entire week
// ie. 17/11/2019 (this sunday)
- 7
// rewind back to the sunday before this
// sunday, ie. 10/11/2019
Hopefully this explanation explains what the numbers at the end are doing. + 1
takes you from the Saturday of last week to the Sunday of the current week (what I would call "this Sunday"), and - 6
takes you back through last week to what I would call "last Sunday."
See here:
Get last Sunday in Month - JavaScript
For getting the last Sunday of a month, create a new date for the first of next month (so take month, because in JS month is 0-11). For the actual month you had to subtract 1 for the previous month and plus 1 for next month at 12:00 to avoid conflicts with summer/wintertime.
Get the weekday with getDate
(note 0-6 in Javascript is from Sunday to Saturday) and if this day is a Sunday (=0) then assign 7. Build a new date from the first of next month, subtract the day difference, and set it as new date. By using this trick you get the last Sunday of a month.
If you only want the day instead of the date-string then just use lastSunday.getDate()
.
Note: If you use the date-object think of that the time is normally set to 12:00 but through sumer/wintertimechange it could be 11:00 or 13:00. But this shouldn't matter because you wanted only the date and not the time.
function lastSunday(year, month) {
var date = new Date(year,month,1,12);
let weekday = date.getDay();
let dayDiff = weekday===0 ? 7 : weekday;
let lastSunday = date.setDate(date.getDate() - dayDiff);
return date.toDateString();
}
for (let i=1; i<=12; i++) {
console.log(lastSunday(2020,i));
}
Get the date of last Sunday in another column for current date in python
Deriving from get the last sunday and saturday's date in python, you can do:
from datetime import datetime, timedelta
df['Last Sunday'] = (datetime.now()
-timedelta(days=((datetime.now().isoweekday() + 1) % 7))
).strftime('%m/%d/%Y')
Find last sunday
SELECT
DATEADD(day,DATEDIFF(day,'19000107',DATEADD(month,DATEDIFF(MONTH,0,GETDATE() /*YourValuehere*/),30))/7*7,'19000107')
Edit: A correct, final, working answer from my colleague.
Previous Monday & previous Sunday's date based on today's date
Easy:
--start of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 0)
--end of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6, GETDATE()), 6)
EDIT:
The below will handle the Sunday date issue.
DECLARE @input varchar(10)
--SET @input = '9/9/2012' -- simulates a Sunday
SET @input = GETDATE()
--start of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 0)
--end of last week
SELECT DATEADD(wk, DATEDIFF(wk, 6,
CASE DATEPART(dw,@input)
WHEN 1 THEN DATEADD(d,-1,@input)
ELSE @input
END
), 6)
How to get last Sunday and Sunday before last using MySQL?
An option:
SELECT
`dates`.`date` - INTERVAL WEEKDAY(`dates`.`date`) % 7 + 1 DAY `Last`,
`dates`.`date` - INTERVAL WEEKDAY(`dates`.`date`) % 7 + 8 DAY `Previous`
FROM (
SELECT '2019-06-04' `date`
) `dates`;
See dbfiddle.
How to find Previous Sunday in R
Here is one approach:
d <- as.Date("2015-03-18")
prev.days <- seq(d-6,d,by='day')
prev.days[weekdays(prev.days)=='Sunday']
# [1] "2015-03-15"
Calculating previous Sunday Javascript
I just noticed your real problem:
Sunday might be in the last month.
Therefore the Sunday.getDate()-7 is positive, not changing the LWSunday Month property...
So you also need to change the month ( in some cases) :
LWSunday.setDate(Sunday.getDate() - 7);
if(Sunday.getMonth()!==today.getMonth()){
LWSunday.setMonth(LWSunday.getMonth()-1);
}
//same for year...
console.log('last week sunday = ' + LWSunday);
OR you take the whole date:
var LWSunday = new Date(Sunday.getTime() - 7*24*3600*1000);
http://jsbin.com/yevutabuxe/edit?console
Related Topics
How to Save the Result of a SQL Query into a Variable in Vba
How to Concatenate All Columns in a Select with SQL Server
Select Distinct Is Slower Than Expected on My Table in Postgresql
How to Design a Schema Where the Columns of a Table Are Not Fixed
Access a Column Aliases in the Where Clause in Postgresql
Job Queue as SQL Table with Multiple Consumers (Postgresql)
SQL Update to the Sum of Its Joined Values
SQL Query for Parent Child Relationship
How to Use Update Trigger to Update Another Table
Connect SQL Server in Ruby on Rails
How to Create Daylight Savings Time Start and End Function in SQL Server
Update Multiple Columns in a Trigger Function in Plpgsql
SQL Server: Two-Level Group by with Xml Output
SQL Server - Possible Pivot Solution
In SQL Server, When Should You Use Go and When Should You Use Semi-Colon ;
How to Delete Rows in Tables That Contain Foreign Keys to Other Tables