Previous Monday & Previous Sunday's Date Based on Today's Date

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:

Sample Image

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



Leave a reply



Submit