JSFiddle - React, Tailwind, and code Playground

by Sascha

HTML

<div id="result">

</div>

JavaScript

/* fun with dates :-)
 1. get the time from start of last week until end of last week
 2. get the time from start of this week until now
 3. get the time -48 hours until -24 hours from now
 4. get the time -24 hours from now until now
 --> return all as ISO strings
*/ 
var date = new Date();
var weekDay = date.getDay();
var month= date.getMonth()+1;
var day= date.getDate();
var year= date.getFullYear();
var dateBase= new Date(month+'/'+day+'/'+year);

// calculate how many days need to be subtracted until the beginning of the week
if (weekDay==0) weekDay=7;
var offset= weekDay-1;

var todayMorning= dateBase.toISOString();
dateBase.setDate(day-1);
var yesterdayMorning= dateBase.toISOString();

dateBase.setDate(day-offset-6);
var startLastWeek= dateBase.toISOString();
dateBase.setDate(day-offset);
var startThisWeek= dateBase.toISOString();
var now= date.toISOString();

$('#result').append(startLastWeek + ' - '+startThisWeek+'<br />'+startThisWeek+' - '+now+'<br />');
$('#result').append(yesterdayMorning + ' - '+todayMorning+'<br />'+todayMorning+' - '+now);