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)
JavaScript for getting the previous Monday
I think your math is just a little off, and I tidied your syntax;
function getPreviousMonday()
{
var date = new Date();
var day = date.getDay();
var prevMonday = new Date();
if(date.getDay() == 0){
prevMonday.setDate(date.getDate() - 7);
}
else{
prevMonday.setDate(date.getDate() - (day-1));
}
return prevMonday;
}
That way you always get the last Monday that happened (which is 7 days ago if today is Monday)
How to get Date of previous Monday and Previous sunday in Datastudio with calculated fields?
It can be achieved by using either of the following sets of Calculated Fields:
- Current Week: #1 and #2
- Week Minus 1: #3 and #4
1) Sunday: Current Week
DATETIME_TRUNC(CURRENT_DATE(), WEEK)
2) Monday: Current Week
DATETIME_TRUNC(CURRENT_DATE(), ISOWEEK)
3) Sunday: Week -1
DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATE(), WEEK), INTERVAL 1 WEEK)
4) Monday: Week -1
DATETIME_SUB(DATETIME_TRUNC(CURRENT_DATE(), ISOWEEK), INTERVAL 1 WEEK)
Editable Google Data Studio Report and a GIF to elaborate:
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
Given a date, how can I get the previous Monday in UTC format regardless of time zone?
Multiply the unix timestamp by 1000, and use the UTC methods like getUTCDate
instead of getDate
, setUTCHours
instead of setHours
etc..
Of course to return as unix time, just divide by 1000.
eg.
function findMonday(unixTimeStamp) {
let startDate = new Date(unixTimeStamp * 1000);
let startDay = startDate.getUTCDay();
let diff = startDate.getUTCDate() - startDay + (startDay === 0 ? -6 : 1);
let monday = new Date(startDate.setUTCDate(diff));
monday.setUTCHours(0, 0, 0, 0);
monday = new Date(monday).valueOf();
return monday;
}
const monday = findMonday(1655402413);
const unixMonday = Math.trunc(monday / 1000);
console.log('The Date: ' + new Date(monday).toISOString());
console.log('Unix time: ' + unixMonday);
Related Topics
How to Get the Full Resultset from Ssms
Get Values from First and Last Row Per Group
How to Implement SQL Intersect and Minus Operations in Ms Access
How to Transform Vertical Data into Horizontal Data with SQL
Left Join Query Not Returning All Rows in First Table
Database Naming Conventions by Microsoft
Use Variable with Top in Select Statement in SQL Server Without Making It Dynamic
How to Delete from a Table Where Id Is in a List of Ids
How to Backup and Restore a Database as a Copy on the Same Server
How to Drop a Default Value or Similar Constraint in T-Sql
T-SQL Skip Take Stored Procedure
How to Use a Dynamic Parameter in a in Clause of a JPA Named Query
SQL Server: Use Parameter in Create Database
Find Last Row in Group by Query-SQL Server
How to Select a Record and Update It, with a Single Queryset in Django