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(dateBase.getDate() - 1);
var yesterdayMorning = dateBase.toISOString();

dateBase.setDate(dateBase.getDate() - offset);
var startThisWeek = dateBase.toISOString();
dateBase.setDate(dateBase.getDate() - 7);
var startLastWeek = dateBase.toISOString();

var now = date.toISOString();

$('#result')
  .append('Last week: ' + startLastWeek + ' - '+startThisWeek+'<br />')
  .append('This week: ' + startThisWeek+' - '+now+'<br />')
  .append('Yesterday: ' + yesterdayMorning + ' - '+todayMorning+'<br />')
  .append('Today: '+todayMorning+' - '+now);